Conditional Statements in JavaScript

by anupmaurya
0 comment
JavaScript

In this article, you’ll learn about what is Conditional Statements in JavaScript, how to use Conditional Statement and when to use and more.

What is Conditional Statement in JavaScript?

Conditional statements are used when you need to make decision based on different conditions. Each condition works on a Boolean value that can be true or false.

In JavaScript, there are their so many conditional statements. We will discuss each of them one by one.

Types of Conditionals Statement

  1. if statement
  2. if-else Statements
  3. An else-if statement
  4. The Switch Statement

1. If statement

The if statement is used when we want a block of code to be run as long as the condition it true.

Let’s understand with example,

if (4 * 4 = 16) { console.log('This will run!') } // 'This will run!'
Code language: JavaScript (javascript)

2. If-else statement

The if-else statement is used when we want a block of code to be run as long as the condition is true and conditions don’t satisfies else block will run.

Let’s understand with example,

if (4 * 7 < 16) { console.log('This will not run!') } else { console.log('This will run!') }
Code language: JavaScript (javascript)

As in above case, condition doesn’t satisfies else block will run.

3.An else-if statement

An else-if executes the block that matches one of several conditions, or an else block executes if no conditions match. The else-if statement allows JavaScript to execute a block of code based on several conditions.

Let’s understand with example,

if( a == 10 ) { console.log("Value of a is 10\n" ); // if condition is true then this block runs } else if( a == 20 ) { console.log("Value of a is 20\n" ); // if else if condition is true then this block runs } else if( a == 30 ) { console.log("Value of a is 30\n" ); // if else if condition is true then this block runs } else { console.log("None of the values is matching\n" ); //if none of the conditions is true }
Code language: JavaScript (javascript)

4. The Switch Statement

Switch statements serve the same purpose as if statements, however, they are somewhat different from if statement. switch() first takes in an expression and evaluates it once. If any of the preceding cases match the expression, then the return statement within that case will be executed and if none of the case match, default block will executes.

Let’s understand with example,

function getDayOfTheWeek(num) { switch (num) { case 1: console.log('Monday'); break; case 2: console.log('Tuesday'); break; case 3: console.log('Wednesday'); break; case 4: console.log('Thursday'); break; case 5: console.log('Friday'); break; case 6: console.log('Saturday'); break; case 7: console.log('Sunday'); break; default: return 'None of the days match'; } } getDayOfTheWeek(4);
Code language: JavaScript (javascript)

The break statements are an essential part of the switch block because they specify where the code should stop executing. If you miss out a break statement, the code execution will continue and execute all the other code blocks after the first match. This is rarely what you’ll want to happen.

Having fun learning JavaScript!

You may also like