Google News
logo
CoffeeScript Interview Questions
CoffeeScript is a lightweight programing language that compiles into JavaScript. It provides simple and easy to learn syntax avoiding the complex syntax of JavaScript. CoffeeScript is influenced by JavaScript, Ruby, YAML, Haskell, Perl, Python and has influenced MoonScript, LiveScript, and JavaScript.
 
The CoffeeScript language is designed by Jeremy Ashkenas. It was first introduced on 13 December 2009 on the git when Jeremy Ashkenas made the first git commit of CoffeeScript. On February 21, 2010, the first pure CoffeeScript compiler was released earlier the CoffeeScript compiler was in Ruby language.
It follows two rules there are :

1. Indentation rule :
* No Parenthesis
* Short Function declaration
* Define Character per line limit

2. Whitespace rule : CoffeeScript is very sensitive towards white space also no curly braces are allowed in script.
The command-line version of coffee is available as a Node.js utility, requiring Node 6 or later. The core compiler however, does not depend on Node, and can be run in any JavaScript environment, or in the browser (see Try CoffeeScript).
 
To install, first make sure you have a working copy of the latest stable version of Node.js. You can then install CoffeeScript globally with npm:
npm install --global coffeescript
This will make the coffee and cake commands available globally.
 
If you are using CoffeeScript in a project, you should install it locally for that project so that the version of CoffeeScript is tracked as one of your project’s dependencies. Within that project’s folder:
npm install --save-dev coffeescript
The coffee and cake commands will first look in the current folder to see if CoffeeScript is installed locally, and use that version if so. This allows different versions of CoffeeScript to be installed globally and locally.
 
If you plan to use the --transpile option (see Transpilation) you will need to also install @babel/core either globally or locally, depending on whether you are running a globally or locally installed version of CoffeeScript.
The biggest change in CoffeeScript 2 is that now the CoffeeScript compiler produces modern JavaScript syntax (ES6, or ES2015 and later). A CoffeeScript => becomes a JS =>, a CoffeeScript class becomes a JS class and so on. Major new features in CoffeeScript 2 include async functions and JSX. You can read more in the announcement.
 
There are very few breaking changes from CoffeeScript 1.x to 2; we hope the upgrade process is smooth for most projects.
Once installed, you should have access to the coffee command, which can execute scripts, compile .coffee files into .js, and provide an interactive REPL. The coffee command takes the following options :

Option Description
-c, --compile Compile a .coffee script into a .js JavaScript file of the same name.
-t, --transpile Pipe the CoffeeScript compiler’s output through Babel before saving or running the generated JavaScript. Requires @babel/core to be installed, and options to pass to Babel in a .babelrc file or a package.json with a babel key in the path of the file or folder to be compiled. See Transpilation.
-m, --map Generate source maps alongside the compiled JavaScript files. Adds sourceMappingURL directives to the JavaScript as well.
-M, --inline-map Just like --map, but include the source map directly in the compiled JavaScript files, rather than in a separate file.
-i, --interactive Launch an interactive CoffeeScript session to try short snippets. Identical to calling coffee with no arguments.
-o, --output [DIR] Write out all compiled JavaScript files into the specified directory. Use in conjunction with --compile or --watch.
-w, --watch Watch files for changes, rerunning the specified command when any file is updated.
-p, --print Instead of writing out the JavaScript as a file, print it directly to stdout.
-s, --stdio Pipe in CoffeeScript to STDIN and get back JavaScript over STDOUT. Good for use with processes written in other languages. An example:
cat src/cake.coffee | coffee -sc
-l, --literate Parses the code as Literate CoffeeScript. You only need to specify this when passing in code directly over stdio, or using some sort of extension-less file name.
-e, --eval Compile and print a little snippet of CoffeeScript directly from the command line. For example:
coffee -e "console.log num for num in [10..1]"
-r, --require [MODULE] require() the given module before starting the REPL or evaluating the code given with the --eval flag.
-b, --bare Compile the JavaScript without the top-level function safety wrapper.
--no-header Suppress the “Generated by CoffeeScript” header.
--nodejs The node executable has some useful options you can set, such as --debug, --debug-brk, --max-stack-size, and --expose-gc. Use this flag to forward options directly to Node.js. To pass multiple flags, use --nodejs multiple times.
--ast Generate an abstract syntax tree of nodes of the CoffeeScript. Used for integrating with JavaScript build tools.
--tokens Instead of parsing the CoffeeScript, just lex it, and print out the token stream. Used for debugging the compiler.
-n, --nodes Instead of compiling the CoffeeScript, just lex and parse it, and print out the parse tree. Used for debugging the compiler.
To make things easy, CoffeeScript has built-in support for the popular Babel transpiler. You can use it via the --transpile command-line option or the transpile Node API option. To use either, @babel/core must be installed in your project :
npm install --save-dev @babel/core
Or if you’re running the coffee command outside of a project folder, using a globally-installed coffeescript module, @babel/core needs to be installed globally :
npm install --global @babel/core
By default, Babel doesn’t do anything—it doesn’t make assumptions about what you want to transpile to. You need to provide it with a configuration so that it knows what to do. One way to do this is by creating a .babelrc file in the folder containing the files you’re compiling, or in any parent folder up the path above those files. (Babel supports other ways, too.) A minimal .babelrc file would be just { "presets": ["@babel/env"] }. This implies that you have installed @babel/preset-env :
npm install --save-dev @babel/preset-env  # Or --global for non-project-based usage
Functions are defined by an optional list of parameters in parentheses, an arrow, and the function body. The empty function looks like this: ->
square = (x) -> x * x
cube   = (x) -> square(x) * x
var cube, square;
​
square = function(x) {
  return x * x;
};
​
cube = function(x) {
  return square(x) * x;
};​
​
Functions may also have default values for arguments, which will be used if the incoming argument is missing (undefined).
fill = (container, liquid = "coffee") ->
  "Filling the #{container} with #{liquid}..."
var fill;
​
fill = function(container, liquid = "coffee") {
  return `Filling the ${container} with ${liquid}...`;
};​
​
 
In CoffeeScript, comments are denoted by the # character to the end of a line, or from ### to the next appearance of ###. Comments are ignored by the compiler, though the compiler makes its best effort at reinserting your comments into the output JavaScript after compilation.
###
Fortune Cookie Reader v1.0
Released under the MIT License
###
​
sayFortune = (fortune) ->
  console.log fortune # in bed!
​
/*
Fortune Cookie Reader v1.0
Released under the MIT License
*/
var sayFortune;
​
sayFortune = function(fortune) {
  return console.log(fortune); // in bed!
};
​
Inline ### comments make type annotations possible.
Like JavaScript and many other languages, CoffeeScript supports strings as delimited by the " or ' characters. CoffeeScript also supports string interpolation within "-quoted strings, using #{ … }. Single-quoted strings are literal. You may even use interpolation in object keys.
author = "Wittgenstein"
quote  = "A picture is a fact. -- #{ author }"
​
sentence = "#{ 22 / 7 } is a decent approximation of π"
var author, quote, sentence;
​
author = "Wittgenstein";
​
quote = `A picture is a fact. -- ${author}`;
​
sentence = `${22 / 7} is a decent approximation of π`;​
Multiline strings are allowed in CoffeeScript. Lines are joined by a single space unless they end with a backslash. Indentation is ignored.
mobyDick = "Call me Ishmael. Some years ago --
  never mind how long precisely -- having little
  or no money in my purse, and nothing particular
  to interest me on shore, I thought I would sail
  about a little and see the watery part of the
  world..."
var mobyDick;
​
mobyDick = "Call me Ishmael. Some years ago -- never mind how long precisely -- having little or no money in my purse, 
and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world...";​