CoffeeScript

Uses your system voice; voice and rate in settings.

Q1106819Snapshot

≈ 4 min read

Names in other languages

  • mlകോഫീസ്ക്രിപ്റ്റ്
  • azCoffeeScript
  • caCoffeeScript
  • ČeštinaCoffeeScript
  • daCoffeeScript
  • DeutschCoffeeScript
  • EnglishCoffeeScript
  • EspañolCoffeeScript
  • فارسیکافی‌اسکریپتکافی اسکریپت
  • fiCoffeeScript
  • FrançaisCoffeeScript
  • gaCoffeeScript
  • gswCoffeeScript
  • עבריתCoffeeScript
  • MagyarCoffeeScript
  • hyCoffeeScript
  • ItalianoCoffeeScript
  • 日本語CoffeeScript
  • 한국어CoffeeScript
  • mkCoffeeScript
  • mulCoffeeScript
  • nbCoffeeScript
  • NederlandsCoffeeScript
  • nnCoffeeScript
  • PolskiCoffeeScript
  • PortuguêsCoffeeScript
  • PortuguêsCoffeeScript
  • RomânăCoffeeScript
  • РусскийCoffeeScript
  • scoCoffeeScript
  • skCoffeeScript
  • slCoffeeScript
  • SvenskaCoffeeScript
  • TürkçeCoffeeScript
  • ttCoffeeScript
  • УкраїнськаCoffeeScript
  • vecCoffeeScript
  • yueCoffeeScript
  • 中文CoffeeScript
  • العربيةكافي سكريبت
  • ckbکافیسکریپتکافی سکریپت

Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.

CoffeeScript is a programming language that compiles to JavaScript. It adds syntactic sugar inspired by Ruby, Python, and Haskell in an effort to enhance JavaScript's brevity and readability. Some added features include list comprehension and destructuring assignment. CoffeeScript support is included in Ruby on Rails version 3.1 and Play Framework. In 2011, Brendan Eich referenced CoffeeScript as an influence on his thoughts about the future of JavaScript.

CoffeeScript is a programming language that compiles to JavaScript. It adds syntactic sugar inspired by Ruby, Python, and Haskell in an effort to enhance JavaScript's brevity and readability. Some added features include list comprehension and destructuring assignment. CoffeeScript support is included in Ruby on Rails version 3.1 and Play Framework. In 2011, Brendan Eich referenced CoffeeScript as an influence on his thoughts about the future of JavaScript. let mass = 72; let height = 1.78; let BMI = mass / height**2; if (18.5 With CoffeeScript the interval is directly described: mass = 72 height = 1.78 BMI = mass / height**2 alert 'You are healthy!' if 18.5 To compute the greatest common divisor of two integers with the Euclidean algorithm, in JavaScript one usually needs a while loop: let gcd = (x, y) => { do { [x, y] = [y, x%y]; } while (y !== 0) return x; } Whereas in CoffeeScript one can use until instead: gcd = (x, y) -> [x, y] = [y, x%y] until 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 library is: $(document).ready(function() { // Initialization code goes here }); Or even just: $(function() { // Initialization code goes here }); In CoffeeScript, the function keyword is replaced by the -> symbol, and indentation is used instead of curly braces, as in other off-side rule 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. -- #{ author }" sentence = "#{ 22 / 7 } is a decent approximation of π" Any for loop can be replaced by a list comprehension; 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 [1..10] when n%2 is 1 Alternatively, there is: alert n*n for n in [1..10] by 2 A linear search 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 which makes reasoning about code more difficult and error-prone in some basic programming patterns established by and taken for granted since procedural programming 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() { var foo = "bar"; console.log(`foo = ${foo}`); } // ... } 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.

Text derived from https://en.wikipedia.org/w/index.php?oldid=1371234827 under CC BY-SA 4.0; modifications have been made.