Câu lệnh
if [condition1]
statement1
else if [condition2]
statement2
else if [condition3]
statement3
// …
else
statementN
1 thực thi một câu lệnh nếu một điều kiện cụ thể là đúng. Nếu điều kiện sai, một câu lệnh khác trong mệnh đề tùy chọn if [condition1]
statement1
else if [condition2]
statement2
else if [condition3]
statement3
// …
else
statementN
2 sẽ được thực hiệnif [condition]
statement1
// With an else clause
if [condition]
statement1
else
statement2
if [condition1]
statement1
else if [condition2]
statement2
else if [condition3]
statement3
// …
else
statementN
3Một biểu thức được coi là trung thực hoặc sai
if [condition1]
statement1
else if [condition2]
statement2
else if [condition3]
statement3
// …
else
statementN
4Tuyên bố được thực hiện nếu điều kiện là true. Có thể là bất kỳ câu lệnh nào, kể cả câu lệnh
if [condition1]
statement1
else if [condition2]
statement2
else if [condition3]
statement3
// …
else
statementN
5 lồng nhau. Để thực hiện nhiều câu lệnh, hãy sử dụng câu lệnh khối [if [condition1]
statement1
else if [condition2]
statement2
else if [condition3]
statement3
// …
else
statementN
6] để nhóm các câu lệnh đó. Để thực thi không có câu lệnh nào, hãy sử dụng câu lệnh rỗngif [condition1]
statement1
else if [condition2]
statement2
else if [condition3]
statement3
// …
else
statementN
7Tuyên bố được thực thi nếu
if [condition1]
statement1
else if [condition2]
statement2
else if [condition3]
statement3
// …
else
statementN
3 là sai và tồn tại mệnh đề if [condition1]
statement1
else if [condition2]
statement2
else if [condition3]
statement3
// …
else
statementN
2. Có thể là bất kỳ câu lệnh nào, bao gồm các câu lệnh khối và các câu lệnh if [condition1]
statement1
else if [condition2]
statement2
else if [condition3]
statement3
// …
else
statementN
5 lồng nhau khácNhiều câu lệnh
if [condition1]
statement1
else if [condition2]
statement2
else if [condition3]
statement3
// …
else
statementN
1 có thể được lồng vào nhau để tạo mệnh đề if [condition1]
statement1
else
if [condition2]
statement2
else
if [condition3]
statement3
// …
2. Lưu ý rằng không có từ khóa if [condition1]
statement1
else
if [condition2]
statement2
else
if [condition3]
statement3
// …
3 [trong một từ] trong JavaScriptif [condition1]
statement1
else if [condition2]
statement2
else if [condition3]
statement3
// …
else
statementN
Để xem nó hoạt động như thế nào, đây là giao diện của nó nếu lồng được thụt vào đúng cách
if [condition1]
statement1
else
if [condition2]
statement2
else
if [condition3]
statement3
// …
Để thực thi nhiều câu lệnh trong một mệnh đề, hãy sử dụng một câu lệnh khối [_______0_______6] để nhóm các câu lệnh đó
if [condition] {
statements1
} else {
statements2
}
Không sử dụng các khối có thể dẫn đến hành vi khó hiểu, đặc biệt nếu mã được định dạng thủ công. Ví dụ
function checkValue[a, b] {
if [a === 1]
if [b === 2]
console.log["a is 1 and b is 2"];
else
console.log["a is not 1"];
}
Mã này có vẻ vô hại — tuy nhiên, việc thực thi
if [condition1]
statement1
else
if [condition2]
statement2
else
if [condition3]
statement3
// …
5 sẽ ghi "a không phải là 1". Điều này là do trong trường hợp dangling else, mệnh đề if [condition1]
statement1
else if [condition2]
statement2
else if [condition3]
statement3
// …
else
statementN
2 sẽ được nối với mệnh đề if [condition1]
statement1
else if [condition2]
statement2
else if [condition3]
statement3
// …
else
statementN
5 gần nhất. Do đó, mã ở trên, với thụt đầu dòng thích hợp, sẽ giống nhưfunction checkValue[a, b] {
if [a === 1]
if [b === 2]
console.log["a is 1 and b is 2"];
else
console.log["a is not 1"];
}
Nói chung, cách tốt nhất là luôn sử dụng các câu lệnh khối, đặc biệt là trong mã liên quan đến các câu lệnh
if [condition1]
statement1
else if [condition2]
statement2
else if [condition3]
statement3
// …
else
statementN
5 lồng nhaufunction checkValue[a, b] {
if [a === 1] {
if [b === 2] {
console.log["a is 1 and b is 2"];
}
} else {
console.log["a is not 1"];
}
}
Đừng nhầm lẫn giữa các giá trị Boolean nguyên thủy
if [condition1]
statement1
else
if [condition2]
statement2
else
if [condition3]
statement3
// …
9 và if [condition] {
statements1
} else {
statements2
}
0 với tính xác thực hoặc tính sai lệch của đối tượng if [condition] {
statements1
} else {
statements2
}
1. Bất kỳ giá trị nào không phải là if [condition] {
statements1
} else {
statements2
}
0, if [condition] {
statements1
} else {
statements2
}
3, if [condition] {
statements1
} else {
statements2
}
4, if [condition] {
statements1
} else {
statements2
}
5, if [condition] {
statements1
} else {
statements2
}
6, if [condition] {
statements1
} else {
statements2
}
7 hoặc chuỗi rỗng [if [condition] {
statements1
} else {
statements2
}
8] và bất kỳ đối tượng nào, kể cả đối tượng Boolean có giá trị là if [condition] {
statements1
} else {
statements2
}
0, đều được coi là trung thực khi được sử dụng làm điều kiện. Ví dụconst b = new Boolean[false];
if [b] // this condition is truthy
statement
if [cipherChar === fromChar] {
result += toChar;
x++;
} else {
result += clearChar;
}
Lưu ý rằng không có cú pháp
if [condition1]
statement1
else
if [condition2]
statement2
else
if [condition3]
statement3
// …
3 trong JavaScript. Tuy nhiên, bạn có thể viết nó với khoảng cách giữa if [condition1]
statement1
else if [condition2]
statement2
else if [condition3]
statement3
// …
else
statementN
2 và if [condition1]
statement1
else if [condition2]
statement2
else if [condition3]
statement3
// …
else
statementN
5if [x > 50] {
/* do something */
} else if [x > 5] {
/* do something */
} else {
/* do something */
}
Bạn hầu như không bao giờ nên có một
if [condition1]
statement1
else if [condition2]
statement2
else if [condition3]
statement3
// …
else
statementN
1 với một nhiệm vụ như function checkValue[a, b] {
if [a === 1]
if [b === 2]
console.log["a is 1 and b is 2"];
else
console.log["a is not 1"];
}
4 như một điều kiệnif [condition1]
statement1
else if [condition2]
statement2
else if [condition3]
statement3
// …
else
statementN
0Tuy nhiên, trong trường hợp hiếm hoi bạn thấy mình muốn làm điều gì đó như vậy, tài liệu
function checkValue[a, b] {
if [a === 1]
if [b === 2]
console.log["a is 1 and b is 2"];
else
console.log["a is not 1"];
}
5 có một phần với ví dụ hiển thị cú pháp thực tiễn chung tốt nhất mà bạn nên biết và làm theo