
- CoffeeScript Tutorial
- CoffeeScript - Home
- CoffeeScript - Overview
- CoffeeScript - Environment
- CoffeeScript - command-line utility
- CoffeeScript - Syntax
- CoffeeScript - Data Types
- CoffeeScript - Variables
- CoffeeScript - Operators and Aliases
- CoffeeScript - Conditionals
- CoffeeScript - Loops
- CoffeeScript - Comprehensions
- CoffeeScript - Functions
- CoffeeScript Object Oriented
- CoffeeScript - Strings
- CoffeeScript - Arrays
- CoffeeScript - Objects
- CoffeeScript - Ranges
- CoffeeScript - Splat
- CoffeeScript - Date
- CoffeeScript - Math
- CoffeeScript - Exception Handling
- CoffeeScript - Regular Expressions
- CoffeeScript - Classes and Inheritance
- CoffeeScript Advanced
- CoffeeScript - Ajax
- CoffeeScript - jQuery
- CoffeeScript - MongoDB
- CoffeeScript - SQLite
- CoffeeScript Useful Resources
- CoffeeScript - Quick Guide
- CoffeeScript - Useful Resources
- CoffeeScript - Discussion
CoffeeScript - if...else statement
The if statement executes the given block of code if the specified Boolean expression is true. What if the Boolean expression is false?
The 'if...else' statement is the next form of control statement that allows CoffeeScript to execute statements in a more controlled way. It will have an else block which executes when the Boolean expression is false.
Syntax
Given below is the syntax of the if-else statement in CoffeeScript. If the given expression is true, then the statements in the if block are executed and if it is false the statements in the else block are executed.
if expression Statement(s) to be executed if the expression is true else Statement(s) to be executed if the expression is false
Flow Diagram

Example
The following example demonstrates how to use the if-else statement in CoffeeScript. Save this code in a file with name if_else_example.coffee
name = "Ramu" score = 30 if score>=40 console.log "Congratulations have passed the examination" else console.log "Sorry try again"
Open the command prompt and compile the .coffee file as shown below.
c:\> coffee -c if_else_example.coffee
On compiling, it gives you the following JavaScript.
// Generated by CoffeeScript 1.10.0 (function() { var name, score; name = "Ramu"; score = 30; if (score >= 40) { console.log("Congratulations have passed the examination"); } else { console.log("Sorry try again"); } }).call(this);
Now, open the command prompt again and run the CoffeeScript file as −
c:\> coffee if_else_example.coffee
On executing, the CoffeeScript file produces the following output.
Sorry try again