Thursday, August 19, 2021

Switch Statement OR if...elseif....else in Javascript

Switch Statement : In Javascript for checking multiple condition based on logic we can use switch statement instead of if elseif else.

// Using Switch Statement in Javascript..

const favColor = 'green';
switch (favColor) {
case 'pink':
console.log(`This is Pink Color`);
break;

case 'red':
console.log(`This is Red Color`);
break;

case 'yellow':
console.log(`This is yellow color`);
break;

default:
console.log(`This is not available color`);
break;

}

Above Logic we can also implement using if...else if...else. See Below Example-

// Using if...elseif...else in This Code

const favColor = 'blue';
if (favColor === 'blue') {
console.log(`This is blue color`);

} else if (favColor === 'green') {
console.log(`This is Green Color`);

} else if (favColor === 'red') {
console.log(`This is Red color`);

} else {
console.log(`Color not found in our database`);
}

0 comments

Post a Comment