js 条件判断语句
在JavaScript中,有多种条件判断语句,包括if...else
,switch
,和ternary operator
。
-
if...else
语句:let num = 10;if (num > 5) {console.log("Number is greater than 5"); } else {console.log("Number is less than or equal to 5"); }
-
switch
语句:let day = 3;switch (day) {case 1:console.log("Monday");break;case 2:console.log("Tuesday");break;case 3:console.log("Wednesday");break;// 你可以添加更多的case语句default:console.log("Invalid day"); }
-
ternary operator
:let num = 10;num > 5 ? console.log("Number is greater than 5") : console.log("Number is less than or equal to 5");