CoffeeScript - Conditionals


Advertisements


While programming, we encounter some scenarios where we have to choose a path from a given set of paths. In such situations, we need conditional statements. Conditional statements help us take decisions and perform right actions.

Following is the general form of a typical decision-making structure found in most of the programming languages.

Decision making structure

JavaScript supports the if statement (including its variants) and switch statement. In addition to the conditionals available in JavaScript, CoffeeScript includes the unless statement, the negation of if, and even more.

Following are the conditional statements provided by CoffeeScript.

S.No. Statement & Description
1 if statement

An if statement consists of a Boolean expression followed by one or more statements. These statements execute when the given Boolean expression is true.

2 if...else statement

An if statement can be followed by an optional else statement, which executes when the Boolean expression is false.

3 unless statement

An unless statement is similar to if with a Boolean expression followed by one or more statements except. These statements execute when a given Boolean expression is false.

4 unless...else statement

An unless statement can be followed by an optional else statement, which executes when a Boolean expression is true.

5 switch statement

A switch statement allows a variable to be tested for equality against a list of values.

The then Keyword in CoffeeScript

The if and unless statements are block statements that are written in multiple lines. CoffeeScript provides the then keyword using which we can write the if and the unless statements in a single line.

Following are the statements in CoffeeScript that are written using then keyword.

S.No. Statement & Description
1 if-then statement

Using the if-then statement we can write the if statement of CoffeeScript in a single line. It consists of a Boolean expression followed by then keyword, which is followed by one or more statements. These statements execute when the given Boolean expression is true.

2 if-then...else statement

The if-then statement can be followed by an optional else statement, which executes when the Boolean expression is false. Using if-then...else statement, we can write the if...else statement in a single line.

3 unless-then statement

Using the unless-then statement, we can write the unless statement of CoffeeScript in a single line. It consists of a Boolean expression followed by then keyword, which is followed by one or more statements. These statements execute when the given Boolean expression is false.

4 unless...then else statement

The unless-then statement can be followed by an optional else statement, which executes when the Boolean expression is true. Using unless-then...else statement, we can write the unless...else statement in a single line.

postfix if and postfix unless Statements

In CoffeeScript, you can also write the if and unless statements having a code block first followed by if or unless keyword as shown below. This is the postfix form of those statements. It comes handy while writing programs in CoffeeScript.

#Postfix if
Statements to be executed if expression

#Postfix unless
Statements to be executed unless expression

show example



Advertisements