Open In App

How to convert CoffeeScript code in JavaScript ?

Last Updated : 13 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

CoffeeScript is a lightweight programming 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 MoonScript, LiveScript, and JavaScript.

Example: The below example is demonstrating the conversion of CoffeeScript code into JavaScript code:

CoffeeScript




# Assignment:
number   = 89
opposite = false
 
# Functions:
cube = (A) -> a * a * a
 
# Conditions:
number = -89 if !opposite


JavaScript is the world’s most popular lightweight, interpreted, compiled programming language. It is also known as a scripting language for web pages. It is well-known for the development of web pages, and many non-browser environments also use it. JavaScript can be used for Client-side developments as well as Server-side development

Javascript




# Assignment:
number = 89;
opposite = false;
 
# Functions:
cube = function (A) {
    return a * a * a;
};
 
# Conditions:
if (!opposite) {
    number = -89;
}


Now let us understand the step procedure of how to convert a CoffeeScript code into JavaScript with the example:

Step 1: Write CoffeeScript code and save the file with evenOrOdd.coffee extension 

Javascript




// Write coffeeScript program to check whether
// given number is even or odd
 
number = 12
if number % 2 == 0
    console.log "number is even"
else
    console.log "number is odd"


Step 2: To run the evenOrOdd.coffee file, you need to type the following command : 

coffee -c evenOrOdd.coffee

Step 3: After successful compilation of the above evenOrOdd.coffee file, the following JavaScript file will generate.

Javascript




(function () {
    var number;
    number = 12;
 
    if (number % 2 === 0) {
        console.log("number is even");
    } else {
        console.log("number is odd");
    }
 
}).call(this);


Output: 

number is even

This is how you can convert CoffeeScript code into JavaScript.

You can refer to any online editor to convert CoffeeScript code into JavaScript.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads