
- 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-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.
Syntax
Following is the syntax of the if-then statement in CoffeeScript.
if expression then Statement(s) to be executed if expression is true
Example
Given below is the example of the if-then statement of CoffeeScript. Save this code in a file with name if_then_example.coffee
name = "Ramu" score = 60 if score>40 then console.log "Congratulations you have passed the examination"
Open the command prompt and compile the .coffee file as shown below.
c:\> coffee -c if_then_example.coffee
On compiling, it gives you the following JavaScript.
// Generated by CoffeeScript 1.10.0 (function() { var name, score; name = "Ramu"; score = 60; if (score > 40) { console.log("Congratulations you have passed the examination"); } }).call(this);
Now, open the command prompt again and run the CoffeeScript file as −
c:\> coffee if_then_example.coffee
On executing, the CoffeeScript file produces the following output.
Congratulations you have passed the exam
coffeescript_conditionals.htm
Advertisements