CoffeeScript | Statements

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

Function = > console.log "Hello World"

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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.
    ###

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.