Introduction
Hey folks, I hope that you must have read my Day 3 Article on #30DaysOfJs. In case, if you haven't then make sure you give it a read by clicking here.
Conditionals
Conditional Statements are the type of statements in JavaScript that are used for executing decisions based on different conditions. By default, statements in JavaScript are executed from top to bottom. The flow of logic can be altered in two ways:
Conditional Execution: A block of statements that get executed if a certain expression is true.
Repetitive Execution: A block of statements that will repeatedly get executed as long as a certain expression is true. Some of the statements are:
if
if else
if else if else
switch
ternary operator
If
In JavaScript, the keyword if is to used check if a condition is true and then executes the block code. To create an if condition, we need an if keyword, inside a parenthesis and a block of code inside a curly bracket({}).
Syntax:
if (condition) {
// Code
}
For Example:
name = 'Shivank';
if (name.length > 0) {
console.log(`${name} is my name.`);
}
In the above example, as the length of the string is greater than zero the block of code got executed. However, if the condition would have been false then we wouldn't be able to see any results.
If Else
In JavaScript, the keyword if else checks if the condition is true then executes the first block of code otherwise the else condition would be executed.
Syntax:
if (condition) {
// Code for true condition
} else {
// Code for false condition
}
For Example:
let num = 3;
if (num > 0) {
console.log(`${num} is a positive number`);
} else {
console.log(`${num} is a negative number`);
}
// 3 is a positive number
let num = -3
if (num > 0) {
console.log(`${num} is a positive number`);
} else {
console.log(`${num} is a negative number`);
}
// -3 is a negative number
In the second example, as the given number is false, therefore, the else statement gets executed.
If Else If Else
In JavaScript, whenever we need to cater to multiple conditions under if else statements then we use else if condition.
Syntax:
if (condition) {
// Code
} else if (condition) {
// Code
} else {
// Code
}
For Example:
let weather = 'sunny';
if (weather === 'rainy') {
console.log('You need a rain coat.');
} else if (weather === 'cloudy') {
console.log('It might be chilly, you need a jacket.');
} else if (weather === 'sunny') {
console.log('Take an umbrella, it is quite hot.');
} else {
console.log('No need for rain coat.');
}
Switch
Switch statements are an alternative for if else if else statements. It starts with a switch keyword followed by a parenthesis and block of code. Inside the block, we have different cases and the case block runs if the value in the switch statement parenthesis matches the case value. In this, we also use the break statement to terminate the execution after the condition has been satisfied. We also have a default block, which runs if all the cases don't satisfy the condition.
Syntax:
switch (caseValue) {
case 1:
// code
break
case 2:
// code
break
case 3:
// code
break
default:
// code
}
For Example:
let input = prompt('What day is today ?');
let day = input.toLowerCase();
switch (day) {
case 'monday':
console.log('Today is Monday');
break
case 'tuesday':
console.log('Today is Tuesday');
break
case 'wednesday':
console.log('Today is Wednesday');
break
case 'thursday':
console.log('Today is Thursday');
break
case 'friday':
console.log('Today is Friday');
break
case 'saturday':
console.log('Today is Saturday');
break
case 'sunday':
console.log('Today is Sunday');
break
default:
console.log('It is not a week day.');
}
Ternary Operators
In JavaScript, there is another way of writing conditionals and it is done using Ternary Operators.
To learn more about this topic do refer to the previous articles, in case if you haven't.
I hope that you must have found this article quite helpful. If yes, then do give a read to some of my other articles!
Who knows you might become a great programmer ๐ค!