- CoffeeScript - SQLite
- CoffeeScript - MongoDB
- CoffeeScript - jQuery
- CoffeeScript - Ajax
- CoffeeScript - Classes and Inheritance
- CoffeeScript - Regular Expressions
- CoffeeScript - Exception Handling
- CoffeeScript - Math
- CoffeeScript - Date
- CoffeeScript - Splat
- CoffeeScript - Ranges
- CoffeeScript - Objects
- CoffeeScript - Arrays
- CoffeeScript - Strings
- CoffeeScript - Functions
- CoffeeScript - Comprehensions
- CoffeeScript - Loops
- CoffeeScript - Conditionals
- CoffeeScript - Operators and Aliases
- CoffeeScript - Variables
- CoffeeScript - Data Types
- CoffeeScript - Syntax
- CoffeeScript - command-line utility
- CoffeeScript - Environment
- CoffeeScript - Overview
- CoffeeScript - Home
CoffeeScript Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
CoffeeScript - Conditionals
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.
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 | 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 | An if statement can be followed by an optional else statement, which executes when the Boolean expression is false. |
3 | 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 | An unless statement can be followed by an optional else statement, which executes when a Boolean expression is true. |
5 | A switch statement allows a variable to be tested for equapty against a pst of values. |
The then Keyword in CoffeeScript
The if and unless statements are block statements that are written in multiple pnes. CoffeeScript provides the then keyword using which we can write the if and the unless statements in a single pne.
Following are the statements in CoffeeScript that are written using then keyword.
S.No. | Statement & Description |
---|---|
1 | Using the if-then statement we can write the if statement of CoffeeScript in a single pne. 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 | 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 pne. |
3 | Using the unless-then statement, we can write the unless statement of CoffeeScript in a single pne. 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 | 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 pne. |
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
Advertisements