CoffeeScript | Statements
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.
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.
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:
Let’s take another example of Square function that gives the square of a number as a result:
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.
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. ###
Please Login to comment...