Control Flow in JavaScript
本页目录· 2 条
if...else statement
Example
Example
if (condition_1) {
statement_1;
} else if (condition_2) {
statement_2;
} else if (condition_n) {
statement_n;
} else {
statement_last;
}
//To execute multiple statements, group them within a block statement
if (condition) {
statement_1_runs_if_condition_is_true;
statement_2_runs_if_condition_is_true;
} else {
statement_3_runs_if_condition_is_false;
statement_4_runs_if_condition_is_false;
}let theNumber = Number(prompt("Pick a number"));
if (!Number.isNaN(theNumber)) {
console.log("Your number is the square root of " +
theNumber * theNumber);
}else{
console.log('Your input is not a number');
}fruitType='Apples';
switch (fruitType) {
case 'Oranges':
console.log('Oranges are $0.59 a kilo.');
break;
case 'Apples':
console.log('Apples are $0.32 a kilo.');
break;
case 'Bananas':
console.log('Bananas are $0.48 a kilo.');
break;
default:
console.log('Sorry, we are out of ' + fruitType + '.');
}
console.log("Is there anything else you'd like?");课程目录