CoffeeScript facts for kids
![]() |
|
Paradigms | Multi-paradigm: prototype-based, functional, imperative, scripting |
---|---|
Family | ECMAScript |
Designed by | Jeremy Ashkenas |
Developer | same |
First appeared | December 13, 2009 |
Stable release | |
Typing discipline | dynamic, implicit |
Scope | lexical |
Implementation language | CoffeeScript |
Platform | x86-64 |
OS | Cross-platform |
License | MIT |
Filename extensions | .coffee , .litcoffee |
Influenced by | |
Haskell, JavaScript, Perl, Python, Ruby, YAML | |
Influenced | |
MoonScript, LiveScript, JavaScript |
CoffeeScript is a special programming language that makes writing JavaScript code much easier. Think of it as a shortcut! It takes your CoffeeScript code and turns it into regular JavaScript that computers can understand.
It was created to make JavaScript simpler and quicker to write. It uses ideas from other popular languages like Ruby, Python, and Haskell. This helps programmers write less code to do the same things.
Many big projects have used CoffeeScript. For example, it was supported in Ruby on Rails (a web development tool) and Play Framework. Even Brendan Eich, who helped create JavaScript, said CoffeeScript influenced his ideas for JavaScript's future.
Contents
History of CoffeeScript
How CoffeeScript Started
CoffeeScript was first created by Jeremy Ashkenas. He made the very first version on December 13, 2009. He called it a "mystery language" at the time.
Just a few days later, on December 24, 2009, he released the first official version, 0.1.0. The first version of the program that turned CoffeeScript into JavaScript was written in a language called Ruby.
Growing Up: Self-Hosting
On February 21, 2010, Jeremy released version 0.5. This was a big step! The program that turned CoffeeScript into JavaScript was now written in CoffeeScript itself. This is called "self-hosting."
By this time, many other people started helping with the project on GitHub. It became quite popular, getting hundreds of visitors every day.
Stable Release and Modern Updates
On December 24, 2010, Jeremy Ashkenas announced the release of CoffeeScript 1.0.0. This was the first stable version, meaning it was ready for wider use.
Years later, on September 18, 2017, version 2.0.0 was released. This update helped CoffeeScript work better with the newest versions of JavaScript. It kept CoffeeScript's simple style while making it more compatible with modern web development.
How CoffeeScript Works
Simple Code and Less Typing
CoffeeScript tries to make code shorter and easier to read. It lets you leave out many extra parentheses `()` and curly braces `{}` that JavaScript often needs.
Instead of braces, CoffeeScript uses indentation (spaces at the beginning of a line) to show blocks of code. This is similar to how Python works.
For example, in JavaScript, you might write code like this to check your Body Mass Index (BMI):
let mass = 72;
let height = 1.78;
let BMI = mass / height**2;
if (18.5 <= BMI && BMI < 25) alert('You are healthy!');
In CoffeeScript, the same idea can be written more simply:
mass = 72
height = 1.78
BMI = mass / height**2
alert 'You are healthy!' if 18.5 <= BMI < 25
Notice how it's shorter and easier to read, especially the `if` statement at the end!
Functions and Loops
In JavaScript, you use the `function` keyword to create a function. In CoffeeScript, you just use `->`. Here's a common JavaScript code snippet using the jQuery library:
$(document).ready(function() {
// Initialization code goes here
});
In CoffeeScript, it becomes much shorter:
$(document).ready ->
# Initialization code goes here
CoffeeScript also makes loops simpler. You can use a feature called "list comprehension" to create lists or do things for each item in a list. For example, to find the squares of odd numbers from 1 to 10:
alert n*n for n in [1..10] when n%2 is 1
Or, you can step through numbers:
alert n*n for n in [1..10] by 2
Checking for Missing Information
CoffeeScript has a handy `?` symbol. It quickly checks if a variable has no value (is `null` or `undefined`).
personCheck = ->
if not person? then alert("No person") else alert("Have person")
person = null
personCheck()
person = "Ivan"
personCheck()
This code would first say "No person" because `person` is empty, then "Have person" when `person` has a name.
Text with Variables
CoffeeScript lets you easily put variables directly into text strings. This is called "string interpolation."
author = "Wittgenstein"
quote = "A picture is a fact. -- #{ author }"
sentence = "#{ 22 / 7 } is a decent approximation of π"
The `#{ ... }` part tells CoffeeScript to put the value of the variable inside the text.
Development and Tools
The program that turns CoffeeScript into JavaScript is available as a tool for Node.js. Node.js is a way to run JavaScript code outside of a web browser. This means you can use CoffeeScript to build many different kinds of applications.
There are also online tools where you can try CoffeeScript. The official CoffeeScript.org website has a "Try CoffeeScript" button. You can type CoffeeScript code there and instantly see what it looks like in JavaScript. There's also a site called js2coffee that can translate code both ways.
Special Features
Debugging with Source Maps
When you write code, sometimes there are mistakes, called "bugs." Finding and fixing these bugs is called "debugging." CoffeeScript uses "source maps" to help with this. Source maps let you see exactly where a problem is in your original CoffeeScript code, even though it's running as JavaScript.
Literate Programming
CoffeeScript supports a cool way of writing code called "literate programming." This means you can write your code and your explanations (like comments) in the same file, using Markdown. Markdown is a simple way to format text. The CoffeeScript compiler knows to treat indented parts as code and everything else as comments. This makes it easier to explain your code directly within the file.
Extensions and Related Languages
Iced CoffeeScript is an extended version of CoffeeScript. It adds two new keywords, `await` and `defer`. These help programmers write code that does things in a specific order, especially when waiting for tasks to finish. It makes complex tasks look simpler, almost like a step-by-step list.
Who Uses CoffeeScript?
CoffeeScript has been used by some well-known companies and projects.
- In 2012, Dropbox announced they had rewritten their web code from JavaScript to CoffeeScript. However, they later moved to another language called TypeScript in 2017.
- GitHub, a popular platform for sharing code, once suggested writing new JavaScript code in CoffeeScript.
- The Atom text editor, also created by GitHub, was written using CoffeeScript. Its settings files use a format called CSON, which is like a CoffeeScript version of JSON.
- Pixel Game Maker MV, a tool for making video games, uses CoffeeScript as part of its game development environment.
See also
In Spanish: CoffeeScript para niños
- Haxe
- Nim (programming language)
- Amber Smalltalk
- Clojure
- Dart (programming language)
- Kotlin (programming language)
- LiveScript (programming language)
- Opa (programming language)
- Elm (programming language)
- TypeScript
- PureScript