CoffeeScript Conditional Statements
Last Updated :
07 Dec, 2021
The CoffeeScript programming language is used to write a simpler syntax that compiles into JavaScript before getting executed. In any programming language, conditions play an important role in the execution of code in a particular sequence. In this article, we will see various conditional Statements such as If, Else, Else If, etc. that CoffeeScript supports along with the syntax of conditional statements.
In CoffeeScript, there is no use of parenthesis and curly brackets. Either its functions or multi-line conditional statements are delimited by indentation.
If Statement: If we want to test a condition before executing other statements, then we use the If statement. In CoffeeScript, the "If" statement is written by using the "if" keyword.
Syntax:
if condition
statement
Example:
CoffeeScript
number = 10;
if number>0
console.log("Number is positive")
console.log("Outside if block")
Output: In the above code, the "if" statement gets executed as it holds true value, all the instructions inside it will be executed. Once the "if" block is executed, it will execute further conditions outside the conditional block.
Number is positive
Outside if block
If Else Statement: We cannot use the If statement for every other condition. There can be an alternative condition than the If condition. The other instruction needs to get executed if the “if” condition does not hold true. Here, comes “else” in the picture. Let's see how we use if and else together.
Syntax:
if condition1
statement 1
else
statement 2
Example:
CoffeeScript
number = -10;
if number>=0
console.log("Number is positive")
else
console.log("Number is negative")
Output: In the above code, the “if” condition will be executed, as it is not true, it will execute the next statement which is “else”.
Number is negative
Else If Statement: Sometimes there are multiple statements to get checked in a code. We can check multiple conditions to reach to conclusion using the “Else If” statement as many times according to our requirement.
Syntax:
if condition-1
statement 1
else if condition-2
statement 2
else
statement 3
Example:
CoffeeScript
number = 0;
if number>0
console.log("Number is positive")
else if number == 0
console.log("Number is Zero")
else
console.log("Number is negative")
console.log("Outside the conditional block")
Output: The first “if number>0” condition will be checked. Since it is not true, the “else if” statement will be get executed which is true, and will print “Number is zero” in the console. After that, it will not check further conditions and will come out of the conditional block and execute further statements. In the above code, it will print “Outside the conditional block”.
Number is Zero
Outside the conditional block
Nested If Statement: We use nested "if" statements when we want to check more than one condition and execute particular lines of code.
Syntax:
if condition-1
statement-1
if condition-2
statement-2
else if
statement-3
else
statement-4
else
statement
Example:
CoffeeScript
number = 2;
if number > 0
console.log("Number is positive")
if number % 2 == 0
console.log("Number is also even")
else
console.log("Number is odd")
else if number == 0
console.log("Number is Zero")
else
console.log("Number is negative")
console.log("Outside the conditional block")
Output: In the above code, it checks if the number is positive, and then executes the next “If" statement to check if the number is also even or odd. After checking the conditions, and printing the output, it executes instructions after the conditional block and prints the statement.
Number is positive
Number is also even
Outside the conditional block
What happens if we use the "If" instead of the "Else if" statement?
When we use multiple “if” statements, every “if” statement gets executed whether they are true or not. The statements which are true further executes instructions inside them. But when we use the “Else if” statement, it gets executed only if the “If” statement is false.
Example 1:
CoffeeScript
number = 3;
if number > 0
console.log("Number is positive");
if number > 1
console.log("Number is greater than One");
if number > 2
console.log("Number is even greater than Two");
else if number == 0
console.log("Number is Zero");
else
console.log("Number is negative");
console.log("Outside the conditional block");
Output:
Number is positive
Number is greater than One
Number is even greater than Two
Outside the conditional block
There is a problem with multiple “If” statements, if the last “If” statement does not hold true value, then else statement will get executed after that. Let’s understand this with an example.
Example 2:
CoffeeScript
number = 3;
if number > 0
console.log("Number is positive")
if number > 3
console.log("Number is greater than Three")
else
console.log("Number is less than or Equal to Three")
Output: In the above code, it is checking all the "if" conditions, since the last "if" statement is false, it executes the "else" statement instead of breaking out of the conditional block. That’s where nested statements come into the picture. Let’s see how it solves our problem.
Number is positive
Number is less than or Equal to Three
Example 3:
CoffeeScript
number = 3;
if number > 0
console.log("Number is positive")
if number > 3
console.log("Number is greater than Three")
else
console.log("Number is less than or Equal to Three")
Output: After checking if conditions, it will break through the conditional block instead of executing the "else" statement as in the previous case.
Number is positive
Similar Reads
JavaScript Tutorial
JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development
Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
React Interview Questions and Answers
React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
JavaScript Interview Questions and Answers
JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read
React Tutorial
React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon
8 min read
Domain Name System (DNS)
DNS is a hierarchical and distributed naming system that translates domain names into IP addresses. When you type a domain name like www.geeksforgeeks.org into your browser, DNS ensures that the request reaches the correct server by resolving the domain to its corresponding IP address.Without DNS, w
8 min read
NodeJS Interview Questions and Answers
NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net
15+ min read
HTML Interview Questions and Answers
HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML
14 min read
What is an API (Application Programming Interface)
In the tech world, APIs (Application Programming Interfaces) are crucial. If you're interested in becoming a web developer or want to understand how websites work, you'll need to familiarize yourself with APIs. Let's break down the concept of an API in simple terms.What is an API?An API is a set of
10 min read
Introduction of Firewall in Computer Network
A firewall is a network security device either hardware or software-based which monitors all incoming and outgoing traffic and based on a defined set of security rules it accepts, rejects, or drops that specific traffic. It acts like a security guard that helps keep your digital world safe from unwa
10 min read