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

Syntax

Following is the syntax of the unless-then statement in CoffeeScript.

unless expression then Statement(s) to be executed if expression is false

Example

Given below is the example of the unless-then statement of CoffeeScript. Save the following example in a file with name unless_then_example.coffee

name = "Ramu"
score = 30
unless score>=40 then console.log "Sorry try again"

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

c:\> coffee -c unless_then_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("Sorry try again");
  }

}).call(this);

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

c:\> coffee unless_then_example.coffee

On executing, the CoffeeScript file produces the following output.

Sorry try again
coffeescript_conditionals.htm
Advertisements