| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/if...else | [Back] [Original] |
Get to know MDN better
function testNum(a) {
let result;
if (a > 0) {
result = "positive";
} else {
result = "NOT positive";
}
return result;
}
console.log(testNum(-5));
// Expected output: "NOT positive"
if (condition)
statement1
// else
if (condition)
statement1
else
statement2
if...else else if JavaScript elseif
if (condition1)
statement1
else if (condition2)
statement2
else if (condition3)
statement3
//
else
statementN
if (condition1)
statement1
else
if (condition2)
statement2
else
if (condition3)
statement3
//
{ /* ... */ }
if (condition) {
statements1
} else {
statements2
}
function checkValue(a, b) {
if (a === 1)
if (b === 2)
console.log("a 1 b 2");
else
console.log("a 1");
}
checkValue(1, 3) a 1 else else if
function checkValue(a, b) {
if (a === 1)
if (b === 2)
console.log("a 1 b 2");
else
console.log("a 1");
}
if
function checkValue(a, b) {
if (a === 1) {
if (b === 2) {
console.log("a 1 b 2");
}
} else {
console.log("a 1");
}
}
true false Boolean falseundefinednull0-0NaN "" false
const b = new Boolean(false);
if (b) {
console.log("b "); // b
}
if (cipherChar === fromChar) {
result += toChar;
x++;
} else {
result += clearChar;
}
JavaScript elseif else if
if (x > 50) {
/* */
} else if (x > 5) {
/* */
} else {
/* */
}
if...else x = y
if ((x = y)) {
//
}
x = y;
if (x) {
//
}
| ECMAScript 2027 LanguageSpecification # sec-if-statement |
| Web Proxy Viewer | New URL | Original Page |