- 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 statement
The if statement is the fundamental control statement that allows us to make decisions and execute statements conditionally.
The if statement in CoffeeScript is similar to that we have in JavaScript. The difference is that while writing an if statement in CoffeeScript, there is no need to use parentheses to specify the Boolean condition. Also, instead of curly braces, we separate the body of the conditional statement by using proper indentations.
Syntax
Given below is the syntax of the if statement in CoffeeScript. It contains a keyword if, soon after the if keyword, we have to specify a Boolean expression which is followed by a block of statements. If the given expression is true, then the code in the if block is executed.
if expression Statement(s) to be executed if expression is true
Flow Diagram
Example
The following example demonstrates how to use the if statement in CoffeeScript. Save this code in a file with the name if_example.coffee
name = "Ramu" score = 60 if score>=40 console.log "Congratulations you have passed the examination"
Open the command prompt and compile the .coffee file as shown below.
c:\> coffee -c if_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 shown below.
c:\> coffee if_example.coffee
On executing, the CoffeeScript file produces the following output.
Congratulations you have passed the examination
To Continue Learning Please Login
Login with Google