CoffeeScript
   HOME

TheInfoList



OR:

CoffeeScript is a
programming language A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language. The description of a programming ...
that compiles to
JavaScript JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of Website, websites use JavaScript on the Client (computing), client side ...
. It adds
syntactic sugar In computer science, syntactic sugar is syntax within a programming language that is designed to make things easier to read or to express. It makes the language "sweeter" for human use: things can be expressed more clearly, more concisely, or in an ...
inspired by
Ruby A ruby is a pinkish red to blood-red colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called sa ...
,
Python Python may refer to: Snakes * Pythonidae, a family of nonvenomous snakes found in Africa, Asia, and Australia ** ''Python'' (genus), a genus of Pythonidae found in Africa and Asia * Python (mythology), a mythical serpent Computing * Python (pro ...
, and
Haskell Haskell () is a general-purpose, statically-typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research and industrial applications, Haskell has pioneered a number of programming lan ...
in an effort to enhance JavaScript's brevity and readability. Specific additional features include
list comprehension A list comprehension is a syntactic construct available in some programming languages for creating a list based on existing lists. It follows the form of the mathematical ''set-builder notation'' (''set comprehension'') as distinct from the use of ...
and
destructuring assignment In computer programming, an assignment statement sets and/or re-sets the value stored in the storage location(s) denoted by a variable name; in other words, it copies a value into the variable. In most imperative programming languages, the assi ...
. CoffeeScript support is included in
Ruby on Rails Ruby on Rails (simplified as Rails) is a server-side web application framework written in Ruby under the MIT License. Rails is a model–view–controller (MVC) framework, providing default structures for a database, a web service, and we ...
version 3.1 and
Play Framework Play Framework is an open-source software, open-source web application framework which follows the model–view–controller (MVC) architectural pattern (computer science), architectural pattern. It is written in Scala (programming language), Sc ...
. In 2011,
Brendan Eich Brendan Eich (; born July 4, 1961) is an American computer programmer and technology executive. He created the JavaScript programming language and co-founded the Mozilla project, the Mozilla Foundation, and the Mozilla Corporation. He served ...
referenced CoffeeScript as an influence on his thoughts about the future of JavaScript.


History

On December 13, 2009,
Jeremy Ashkenas Jeremy Ashkenas is a computer programmer known for the creation and co-creation of the CoffeeScript and LiveScript (programming language), LiveScript programming languages respectively, the Backbone.js JavaScript Software framework, framework and ...
made the first
Git Git () is a distributed version control system: tracking changes in any set of files, usually used for coordinating work among programmers collaboratively developing source code during software development. Its goals include speed, data in ...
commit of CoffeeScript with the comment: "initial commit of the mystery language." The compiler was written in Ruby. On December 24, he made the first tagged and documented release, 0.1.0. On February 21, 2010, he committed version 0.5, which replaced the Ruby compiler with a self-hosting version in pure CoffeeScript. By that time the project had attracted several other contributors on
GitHub GitHub, Inc. () is an Internet hosting service for software development and version control using Git. It provides the distributed version control of Git plus access control, bug tracking, software feature requests, task management, continuous ...
, and was receiving over 300 page hits per day. On December 24, 2010, Ashkenas announced the release of stable 1.0.0 to
Hacker News Hacker News (sometimes abbreviated as HN) is a social news website focusing on computer science and entrepreneurship. It is run by the investment fund and startup incubator Y Combinator. In general, content that can be submitted is defined as "any ...
, the site where the project was announced for the first time. On September 18, 2017, version 2.0.0 was introduced, which "aims to bring CoffeeScript into the modern JavaScript era, closing gaps in compatibility with JavaScript while preserving the clean syntax that is CoffeeScript’s hallmark."


Syntax

Almost everything is an
expression Expression may refer to: Linguistics * Expression (linguistics), a word, phrase, or sentence * Fixed expression, a form of words with a specific meaning * Idiom, a type of fixed expression * Metaphorical expression, a particular word, phrase, o ...
in CoffeeScript, for example, if, switch and for expressions (which have no return value in JavaScript) return a value. As in
Perl Perl is a family of two high-level, general-purpose, interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it also referred to its redesigned "sister language", Perl 6, before the latter's name was offici ...
, these control statements also have postfix versions; for example, if can also be written in consequent if condition form. Many unnecessary parentheses and braces can be omitted; for example, blocks of code can be denoted by indentation instead of braces, function calls are implicit, and object literals are often detected automatically. To compute the
body mass index Body mass index (BMI) is a value derived from the mass (weight) and height of a person. The BMI is defined as the body mass divided by the square of the body height, and is expressed in units of kg/m2, resulting from mass in kilograms and he ...
in
JavaScript JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of Website, websites use JavaScript on the Client (computing), client side ...
, one could write: const mass = 72 const height = 1.78 const BMI = mass / height ** 2 if (18.5 <= BMI && BMI < 25) With CoffeeScript the interval is directly described: mass = 72 height = 1.78 BMI = mass / height**2 alert 'You are healthy!' if 18.5 <= BMI < 25 To compute the
greatest common divisor In mathematics, the greatest common divisor (GCD) of two or more integers, which are not all zero, is the largest positive integer that divides each of the integers. For two integers ''x'', ''y'', the greatest common divisor of ''x'' and ''y'' is ...
of two integers with the
Euclidean algorithm In mathematics, the Euclidean algorithm,Some widely used textbooks, such as I. N. Herstein's ''Topics in Algebra'' and Serge Lang's ''Algebra'', use the term "Euclidean algorithm" to refer to Euclidean division or Euclid's algorithm, is an effi ...
, in JavaScript one usually needs a ''while'' loop: gcd = (x, y) => Whereas in CoffeeScript one can use until instead: gcd = (x, y) ->
, y The comma is a punctuation mark that appears in several variants in different languages. It has the same shape as an apostrophe or single closing quotation mark () in many typefaces, but it differs from them in being placed on the baseline ...
= , x%yuntil y is 0 x
The ? keyword quickly checks if a variable is null or undefined : personCheck = -> if not person? then alert("No person") else alert("Have person") person = null personCheck() person = "Ivan" personCheck() This would alert "No person" if the variable is null or undefined and "Have person" if there is something there. A common pre-es6 JavaScript snippet using the
jQuery jQuery is a JavaScript library designed to simplify HTML DOM tree traversal and manipulation, as well as event handling, CSS animation, and Ajax. It is free, open-source software using the permissive MIT License. As of Aug 2022, jQuery is used ...
library is: $(document).ready(function() ) Or even just: $(function() ) In CoffeeScript, the function keyword is replaced by the -> symbol, and indentation is used instead of curly braces, as in other
off-side rule A computer programming language is said to adhere to the off-side rule of syntax if blocks in that language are expressed by their indentation. The term was coined by Peter Landin, possibly as a pun on the offside rule in association football. ...
languages such as Python and Haskell. Also, parentheses can usually be omitted, using indentation level instead to denote a function or block. Thus, the CoffeeScript equivalent of the snippet above is: $(document).ready -> # Initialization code goes here Or just: $ -> # Initialization code goes here Ruby-style string interpolation is included in CoffeeScript. Double-quoted strings allow for interpolated values, using #, and single-quoted strings are literal. author = "Wittgenstein" quote = "A picture is a fact. -- #" sentence = "# is a decent approximation of π" Any ''for'' loop can be replaced by a
list comprehension A list comprehension is a syntactic construct available in some programming languages for creating a list based on existing lists. It follows the form of the mathematical ''set-builder notation'' (''set comprehension'') as distinct from the use of ...
; so that to compute the squares of the positive odd numbers smaller than ten (i.e. numbers whose remainder modulo 2 is 1), one can do: alert n*n for n in ..10when n%2 is 1 Alternatively, there is: alert n*n for n in ..10by 2 A
linear search In computer science, a linear search or sequential search is a method for finding an element within a list. It sequentially checks each element of the list until a match is found or the whole list has been searched. A linear search runs in at ...
can be implemented with a one-liner using the when keyword: names = Ivan", "Joanna", "Nikolay", "Mihaela"linearSearch = (searchName) -> alert(name) for name in names when name is searchName The for ... in syntax allows looping over arrays while the for ... of syntax allows looping over objects. CoffeeScript has been criticized for its unusual scoping rules. In particular, it completely disallows
variable shadowing In computer programming, variable shadowing occurs when a variable declared within a certain scope (decision block, method, or inner class) has the same name as a variable declared in an outer scope. At the level of identifiers (names, rather than ...
which makes reasoning about code more difficult and error-prone in some basic programming patterns established by and taken for granted since
procedural programming Procedural programming is a programming paradigm, derived from imperative programming, based on the concept of the ''procedure call''. Procedures (a type of routine or subroutine) simply contain a series of computational steps to be carried ...
principles were defined. For example, with the following code snippet in JavaScript one does not have to look outside the -block to know for sure that no possible foo variable in the outer scope can be incidentally overridden: // ... function baz() // ... } In CoffeeScript there is no way to tell if the scope of a variable is limited to a block or not without looking outside the block.


Development and distribution

The CoffeeScript compiler has been self-hosting since version 0.5 and is available as a
Node.js Node.js is an open-source server environment. Node.js is cross-platform and runs on Windows, Linux, Unix, and macOS. Node.js is a back-end JavaScript runtime environment. Node.js runs on the V8 JavaScript Engine and executes JavaScript code o ...
utility; however, the core compiler does not rely on Node.js and can be run in any
JavaScript JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of Website, websites use JavaScript on the Client (computing), client side ...
environment. One alternative to the
Node.js Node.js is an open-source server environment. Node.js is cross-platform and runs on Windows, Linux, Unix, and macOS. Node.js is a back-end JavaScript runtime environment. Node.js runs on the V8 JavaScript Engine and executes JavaScript code o ...
utility is the Coffee Maven Plugin, a plugin for the
Apache Maven Maven is a build automation tool used primarily for Java projects. Maven can also be used to build and manage projects written in C#, Ruby, Scala, and other languages. The Maven project is hosted by the Apache Software Foundation, where it was ...
build system. The plugin uses the
Rhino A rhinoceros (; ; ), commonly abbreviated to rhino, is a member of any of the five extant species (or numerous extinct species) of odd-toed ungulates in the family Rhinocerotidae. (It can also refer to a member of any of the extinct species o ...
JavaScript engine written in
Java Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's List ...
. The official site at CoffeeScript.org has a "Try CoffeeScript" button in the menu bar; clicking it opens a modal window in which users can enter CoffeeScript, see the JavaScript output, and run it directly in the browser. The js2coffee site provides bi-directional translation.


Latest additions

* Source maps allow users to debug their CoffeeScript code directly, supporting CoffeeScript tracebacks on run time errors. * CoffeeScript supports a form of
Literate Programming Literate programming is a programming paradigm introduced in 1984 by Donald Knuth in which a computer program is given as an explanation of its logic in a natural language, such as English, interspersed (embedded) with snippets of macros and t ...
, using the .coffee.md or .litcoffee file extension. This allows CoffeeScript source code to be written in
Markdown Markdown is a lightweight markup language for creating formatted text using a plain-text editor. John Gruber and Aaron Swartz created Markdown in 2004 as a markup language that is appealing to human readers in its source code form. Markdown is ...
. The compiler will treat any indented blocks (Markdown's way of indicating source code) as code, and ignore the rest as comments.


Extensions

Iced CoffeeScript is a superset of CoffeeScript which adds two new keywords: await and defer. These additions simplify asynchronous control flow, making the code look more like a
procedural programming Procedural programming is a programming paradigm, derived from imperative programming, based on the concept of the ''procedure call''. Procedures (a type of routine or subroutine) simply contain a series of computational steps to be carried ...
language, eliminating the call-back chain. It can be used on the server side and in the browser.


Adoption

On September 13, 2012,
Dropbox Dropbox is a file hosting service operated by the American company Dropbox, Inc., headquartered in San Francisco, California, U.S. that offers cloud storage, file synchronization, personal cloud, and Client (computing), client software. Dropb ...
announced that their browser-side code base had been rewritten from
JavaScript JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of Website, websites use JavaScript on the Client (computing), client side ...
to CoffeeScript, however it was migrated to
TypeScript TypeScript is a free and open source programming language A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are ...
in 2017.
GitHub GitHub, Inc. () is an Internet hosting service for software development and version control using Git. It provides the distributed version control of Git plus access control, bug tracking, software feature requests, task management, continuous ...
's internal style guide once said "write new JS in CoffeeScript", though it no longer does, and their Atom text editor was also written in the language.Atom source code
github.com. Retrieved on 2021-06-26. Pixel Game Maker MV makes uses of CoffeeScript as part of its game development environment.


See also

*
Haxe Haxe is an open source high-level cross-platform programming language and compiler that can produce applications and source code, for many different computing platforms from one code-base. It is free and open-source software, released under the ...
*
Nim (programming language) Nim is a general-purpose, multi-paradigm, statically typed, compiled systems programming language, designed and developed by a team around Andreas Rumpf. Nim is designed to be "efficient, expressive, and elegant", supporting metaprogramming, ...
* Amber Smalltalk *
Clojure Clojure (, like ''closure'') is a dynamic and functional dialect of the Lisp programming language on the Java platform. Like other Lisp dialects, Clojure treats code as data and has a Lisp macro system. The current development process is comm ...
*
Dart (programming language) Dart is a programming language designed for client development, such as for the web and mobile apps. It is developed by Google and can also be used to build server and desktop applications. It is an object-oriented, class-based, garbage-collec ...
*
Kotlin (programming language) Kotlin () is a cross-platform software, cross-platform, static typing, statically typed, general-purpose programming language, general-purpose programming language with type inference. Kotlin is designed to interoperate fully with Java (programm ...
* LiveScript *
Opa (programming language) Opa is an open-source programming language for developing scalable web applications. It can be used for both client-side and server-side scripting, where complete programs are written in Opa and subsequently compiled to Node.js on the server an ...
*
Elm (programming language) Elm is a Domain-specific language, domain-specific programming language for Declarative programming, declaratively creating web browser-based graphical user interfaces. Elm is purely functional programming, purely functional, and is developed wi ...
*
TypeScript TypeScript is a free and open source programming language A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are ...
*
PureScript PureScript is a strongly-typed, purely-functional programming language that compiles to JavaScript. It can be used to develop web applications, server side apps, and also desktop applications with use of Electron. Its syntax is mostly comparabl ...


References


Further reading

* * * * *


External links

* {{Authority control Dynamic programming languages Programming languages created in 2009 JavaScript programming language family Prototype-based programming languages Software using the MIT license Source-to-source compilers High-level programming languages 2009 software Free software projects