}else if(表达式/条件){ 为真的时候执行的代码 }else{ 为假的时候执行的代码 } // 成绩评价if(num>=90&& num<=100){console.log('优秀'); }elseif(num>=80&& num<90){console.log('良好'); }elseif(num>=60&& num<80){console.log('中等'); }elseif(num<60&& num>=0){console.log('差...
The else statement is another conditional statement in JavaScript. Unlike if, it couldn't be specified alone — there has to be an if statement preceding an else. So what's else used for? The else statement executes a piece of code if the condition in the preceding if statement isn't me...
In JavaScript we have the following conditional statements: Useifto specify a block of code to be executed, if a specified condition is true Useelseto specify a block of code to be executed, if the same condition is false Useelse ifto specify a new condition to test, if the first condit...
// 1.双分支if(表达式1) { 代码块1; }else{ 代码块2; }// 2.多分支if(表达式1) { }elseif(表达式2) { } ...elseif(表达式2) { }else{ } if 嵌套 if(表达式1) {if(表达式2) { }... }... 2、switch语句 switch(表达式) {case值1: 代码块1;break;case值2: 代码块2;break;default:...
else if(mySal == yourSal) { alert("My Salary is equal to your salary"); } Try it We will learn about switch case in the next section. Points to Remember : Use if-else conditional statements to control the program flow. JavaScript includes three forms of if condition: if condition, ...
1. 循环语句的介绍 循环语句就是让一部分代码重复执行,javascript中常用的循环语句有: for while do-...
The JavaScript if – else statements also provides for else if clauses through which we can make additional branches to the condition.Sponsored Linksif (number > 0) { alert("Number is a positive integer"); } else if (number < 0) { alert("Number is a negative integer"); } else { ...
In the end, the choice is yours and I hope this blog helps lead you in the right path to making the most informed decision when to use an if-else statement verses a switch case! Frequently Asked Questions Is switch better than if-else in JavaScript?
Example 2: JavaScript if…else Statementlet age = 17; // if age is 18 or above, you are an adult // otherwise, you are a minor if (age >= 18) { console.log("You are an adult"); } else { console.log("You are a minor"); } // Output: You are a minor Run Code In the...
JavaScript If…Else StatementsIn this tutorial you will learn how to write the decision-making code using if...else...else if conditional statements in JavaScript.JavaScript Conditional StatementsLike many other programming languages, JavaScript also allows you to write code that perform different ...