Open In App

CoffeeScript | Statements

Last Updated : 08 May, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

The syntax of CoffeeScript is simpler to JavaScript and can be easily learned if you have knowledge of JavaScript. It avoids the use of semicolons, curly braces, and variable declarations.

CoffeeScript Statements: The statements of CoffeeScript do not end with semicolons (;). In this language, a new line is considered as a separate statement by the CoffeeScript compiler. Let’s take an example to understand it.

Example:




Name = “Geek”
Age = 19
  
Console.log Name
Console.log Age


Output:

Geek
19

Console.log() is a function that prints the result on the console in JavaScript but in CoffeeScript, we simply use console.log without using any parenthesis. In the same way, two statements can be written in a single line separating with a semicolon as shown below:

Example:




Name = “Geek” ; Age = 19
  
Console.log Name
Console.log Age


Output:
Geek
19

CoffeeScript Variables: In CoffeeScript, the use of the var keyword is exempted. The variables are created by assigning values to them. Like in JavaScript, we declare variable using var keyword.

var a = 10
var b = 20

But in case of CoffeeScript, we declare the variable as:

a = 10
b = 20

Parenthesis: While declaring a function in most of the programming languages, we make use of parenthesis to avoid ambiguity and make code readable. But in CoffeeScript, the parenthesis is not used whereas an arrow mark (->) is used instead of parenthesis while creating functions as shown below.

Example:




Function = > console.log "Hello World"


It is the case sometimes when we need to use parenthesis. For example, while calling such functions as created above to display the result on the console, we call the function as:

Function()  

Let’s take another example of Square function that gives the square of a number as a result:

Example:




Square = (x) -> x*x
  
Console.log Square 4


Output:

16

Curly Braces: Generally, for the code blocks such as functions, loops, we use curly braces but In CoffeeScript, we don’t use curly braces. Instead, the proper indentation should be maintained with whitespaces inside the body. Here is an example of CoffeeScript function, in this, we have used four whitespaces as indentation to separate the statements within the function.

Example:




Function = ->
    Name = "Nimrat"
    Console.log "Hello" + Name
  
Function()


Output:

Hello Nimrat

Comments: In programming languages, comments are used to make code more understandable. The comments in CoffeeScript are similar to that of Ruby language. In CoffeeScript, there are two types of comments as follow:

  • Single-line Comments: When we need to comment on the single line in CoffeeScript, we only need to place a hashtag (#) before starting the line as shown below:
    # This is a single-line comment

    Any line that follows a hashtag is considered as a comment by the compiler, compiling the rest of the code except the comments.

  • Multi-line Comments: When there is a need to comment more than one line, the lines that we need to comment on are wrapped within a pair of three hashtags as shown below:
    ###
    This is how multi lines are commented in CoffeeScript. 
    We can keep as many lines as we want in comments using
    a pair of triple hashtags.
    ###


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads