CoffeeScript is a lightweight 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, and Python and has influenced Moon Script, Live Script, and JavaScript.
In this tutorial, the readers will learn the basic functionality of CoffeeScript with code to build dynamic websites and web applications after mastering it.
Before going through this tutorial, the readers should have prior knowledge of JavaScript, as it is similar to CoffeeScript. It contains additional features list comprehension and destructing assignment.
Installation: You should’ve installed Node.js and NPM before running the commands below and installing CoffeeScript.
Node.js: Node.js is an open-source and cross-platform runtime environment for executing JavaScript code outside a browser. You need to remember that NodeJS is not a framework, and it’s not a programming language
NPM: NPM (Node Package Manager) is the default package manager for Node.js and is written entirely in JavaScript. Developed by Isaac Z. Schlueter, it was initially released on January 12, 2010.
# Install locally for a project: npm install --save-dev coffeescript # Install globally to execute .coffee files anywhere: npm install --global coffeescript
# You can run the Coffeescript by command coffee -c
Example: Create an evenOdd.coffee file and write CoffeeScript code
Javascript
// Write coffeeScript program to check whether // given number is even or odd: number = 13 if number % 2 == 0 console.log "number is even" else console.log "number is odd" |
Step to compile coffee file: You need to type the following command in the terminal to compile the coffee file:
coffee -c evenOdd.coffee
After successful compilation of the above evenOdd.coffee file, you will get a JavaScript code file.
Javascript
( function () { var number; number = 13; if (number % 2 === 0) { console.log( "number is even" ); } else { console.log( "number is odd" ); } }).call( this ); |
Output:
number is odd