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


Following is the syntax of the if-then...else statement in CoffeeScript.

if expression then Statements (for true condition) else Statements (for false condition)


Given below is the example of the if-then...else statement of CoffeeScript. Save this code in a file with name

name = "Ramu"
score = 30
if score>=40 then console.log "Congratulations" else console.log "Sorry try again"

Open the command prompt and compile the .coffee file as shown below.

c:\> coffee -c

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) {
  } else {
    console.log("Sorry try again");


Now, open the command prompt again and run the CoffeeScript file as −

c:\> coffee

On executing, the CoffeeScript file produces the following output.

Sorry try again
Kickstart Your Career

Get certified by completing the course

Get Started