Friday, December 27, 2024
Google search engine
HomeLanguagesJavascriptHow to convert CoffeeScript code in JavaScript ?

How to convert CoffeeScript code in JavaScript ?

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.

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments