0
What is the selection statement in JS?
What is the selection statement in JS?
We use the, selection statement to test a condition and decide the execution of a block of statements based on that condition result.The given condition then decides the execution of a block of statements. If the condition is true, then the block of statements is executed and if it is false, then the block of statements is ignored. Selection
statement are two type.
if (condition)
true block of statement
else
false block of statement
var company = window.prompt('Please enter your company', 'Your company name');
if (company != null) {
document.write('Your company name : ' + company + ' <br/>');
}
else {
document.write('Please enter your company name <br/>');
}
switch(condition)
case 1:
case 1 statement
[break]
Case 2:
case 2 statement
[break]
Case n:
case n statement
[break]
default:
default statement
[break]
var color = window.prompt('Please enter your favorite color name', 'Your favorite color name');
switch (color.toLowerCase()) {
case 'red':
document.write('favorite color name is red <br />');
break;
case 'yellow':
document.write('favorite color name is yellow <br />');
break;
case 'pink':
document.write('favorite color name is pink <br />');
break;
case 'blue':
document.write('favorite color name is blue <br />');
break;
default:
document.write('favorite color name is black <br />');
break;
}