Sẽ có những lúc bạn sẽ muốn viết các lệnh xử lý các quyết định khác nhau trong mã của bạn. Ví dụ: nếu bạn đang mã hóa bot, bạn có thể trả lời với các tin nhắn khác nhau dựa trên một bộ lệnh mà nó nhận được. Trong bài viết này, tôi sẽ giải thích tuyên bố
5 là gì và cung cấp các ví dụ về mã. Chúng tôi cũng sẽ xem xét toán tử điều kiện [ternary] mà bạn có thể sử dụng làm cách viết tắt cho câu lệnh const age = 18;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
5.const age = 18;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
Nếu ... tuyên bố khác trong JavaScript là gì?
const age = 18;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
5 là một loại câu lệnh có điều kiện sẽ thực thi một khối mã khi điều kiện trong câu lệnh const age = 18;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
8 là const age = 18;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
9. Nếu điều kiện là const age = 12;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
0, thì khối const age = 12;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
1 sẽ được thực thi.Các giá trị
const age = 12;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
2 và const age = 12;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
0 được chuyển đổi thành const age = 12;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
4 hoặc const age = 12;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
5 trong & nbsp; ________ 18 câu lệnh.if [condition is true] {
// code is executed
} else {
// code is executed
}
Bất kỳ giá trị nào không được định nghĩa là
const age = 12;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
0 sẽ được coi là const age = 18;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
9 trong JavaScript.Dưới đây là danh sách & nbsp; ____ ____ 20 giá trị:
- false
- 0 [không]
- -0 [không âm]
- 0n [Bigint Zero]
0,if [condition 1 is true] { // code is executed } else if [condition 2 is true] { // code is executed } else { // code is executed }
1,if [condition 1 is true] { // code is executed } else if [condition 2 is true] { // code is executed } else { // code is executed }
2 [Chuỗi trống]if [condition 1 is true] { // code is executed } else if [condition 2 is true] { // code is executed } else { // code is executed }
- null
- chưa xác định
- Nan [không phải là một số]
Ví dụ về nếu ... câu lệnh khác trong JavaScript
Trong ví dụ này, điều kiện cho câu lệnh
const age = 18;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
8 là const age = 12;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
4 vì vậy tin nhắn được in vào bảng điều khiển sẽ là "Nick là người lớn".const age = 18;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
, nếu tôi thay đổi biến if [condition 1 is true] {
// code is executed
} else if [condition 2 is true] {
// code is executed
} else {
// code is executed
}
5 thành nhỏ hơn 18, thì điều kiện sẽ là const age = 12;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
5 và mã sẽ thực thi khối const age = 12;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
1 thay thế.But if I change the
if [condition 1 is true] {
// code is executed
} else if [condition 2 is true] {
// code is executed
} else {
// code is executed
}
5 variable to be less than 18, then the condition would be const age = 12;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
5 and the code would execute the const age = 12;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
1 block instead. const age = 12;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
Examples của nhiều điều kiện [nếu ... nếu ... câu lệnh khác] trong JavaScriptExamples of multiple conditions [if...else if...else statements] in JavaScript
Sẽ có những lúc bạn muốn kiểm tra nhiều điều kiện. Đó là nơi khối
if [condition 1 is true] {
// code is executed
} else if [condition 2 is true] {
// code is executed
} else {
// code is executed
}
8 đi vào.if [condition 1 is true] {
// code is executed
} else if [condition 2 is true] {
// code is executed
} else {
// code is executed
}
Khi câu lệnh
const age = 18;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
8 là const age = 12;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
5, máy tính sẽ chuyển sang câu lệnh if [condition 1 is true] {
// code is executed
} else if [condition 2 is true] {
// code is executed
} else {
// code is executed
}
8. Nếu đó cũng là const age = 12;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
5, thì nó sẽ di chuyển vào khối const age = 12;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
1.Trong ví dụ này, khối
if [condition 1 is true] {
// code is executed
} else if [condition 2 is true] {
// code is executed
} else {
// code is executed
}
8 sẽ được thực thi vì Alice ở độ tuổi từ 18 đến 21.const age = 18;
if [age < 18] {
console.log["Alice is under 18 years old."];
} else if [age >= 18 && age = 18 && age = 18 && age = 18 && age = 18 && age = 18 && age = 18 && age = 18 && age = 18 && age = 18 && age = 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
4, thì khối const age = 18;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
8 sẽ được thực thi. Nếu một hoặc cả hai điều kiện là const age = 12;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
5, thì khối const age = 12;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
1 sẽ được thực thi.Trong ví dụ này, vì tuổi lớn hơn 16 và biến
const pet = "dog";
if [pet === "lizard"] {
console.log["I own a lizard"];
} else if [pet === "dog"] {
console.log["I own a dog"];
} else if [pet === "cat"] {
console.log["I own a cat"];
} else if [pet === "snake"] {
console.log["I own a snake"];
} else if [pet === "parrot"] {
console.log["I own a parrot"];
} else {
console.log["I don't own a pet"];
}
9 là const age = 12;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
4, khối const age = 18;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
8 sẽ chạy. Thông điệp được in vào bảng điều khiển sẽ là "Jerry đủ tuổi để lái và có chiếc xe của riêng mình."const age = 17;
const ownsCar = true;
if [age >= 16 && ownsCar] {
console.log["Jerry is old enough to drive and has his own car."];
} else {
console.log["Jerry does not drive."];
}
nếu tôi thay đổi biến if [condition 1 is true] {
// code is executed
} else if [condition 2 is true] {
// code is executed
} else {
// code is executed
}
5 thành nhỏ hơn 16, thì cả hai điều kiện không còn là const age = 12;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
4 và khối const age = 12;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
1 sẽ được thực thi thay thế.If I change the
if [condition 1 is true] {
// code is executed
} else if [condition 2 is true] {
// code is executed
} else {
// code is executed
}
5 variable to be
less than 16, then both conditions are no longer const age = 12;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
4 and the const age = 12;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
1 block would be executed instead. const age = 13;
const ownsCar = true;
if [age >= 16 && ownsCar] {
console.log["Jerry is old enough to drive and has his own car."];
} else {
console.log["Jerry does not drive."];
}
Trình vận hành logic hoặc [||] và nếu ... các câu lệnh khác trong JavaScriptThe logical OR [||] operator and if...else statements in JavaScript
Trong toán tử logic hoặc [
const pet = "dog";
switch [pet] {
case "lizard":
console.log["I own a lizard"];
break;
case "dog":
console.log["I own a dog"];
break;
case "cat":
console.log["I own a cat"];
break;
case "snake":
console.log["I own a snake"];
break;
case "parrot":
console.log["I own a parrot"];
break;
default:
console.log["I don't own a pet"];
break;
}
5], nếu một hoặc cả hai điều kiện là const age = 12;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
4, thì mã bên trong câu lệnh const age = 18;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
8 sẽ thực thi.Trong ví dụ này, mặc dù biến
const pet = "dog";
switch [pet] {
case "lizard":
console.log["I own a lizard"];
break;
case "dog":
console.log["I own a dog"];
break;
case "cat":
console.log["I own a cat"];
break;
case "snake":
console.log["I own a snake"];
break;
case "parrot":
console.log["I own a parrot"];
break;
default:
console.log["I don't own a pet"];
break;
}
8 được đặt thành const age = 12;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
5, mã bên trong khối const age = 18;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
8 vẫn sẽ thực thi vì biến const age = 17;
const ownsCar = true;
if [age >= 16 && ownsCar] {
console.log["Jerry is old enough to drive and has his own car."];
} else {
console.log["Jerry does not drive."];
}
1 được đặt thành const age = 12;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
4.const boyfriendIsPaying = true;
const isSale = false;
if [boyfriendIsPaying || isSale] {
console.log["Jesse will go shopping."];
} else {
console.log["Jesse will not go shopping."];
}
nếu tôi đã thay đổi giá trị của biến const age = 17;
const ownsCar = true;
if [age >= 16 && ownsCar] {
console.log["Jerry is old enough to drive and has his own car."];
} else {
console.log["Jerry does not drive."];
}
1 thành const age = 12;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
5, sau đó khối const age = 12;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
1 sẽ thực thi vì cả hai điều kiện là const age = 12;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
5.If I were to change the value of the
const age = 17;
const ownsCar = true;
if [age >= 16 && ownsCar] {
console.log["Jerry is old enough to drive and has his own car."];
} else {
console.log["Jerry does not drive."];
}
1 variable to const age = 12;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
5, then
the const age = 12;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
1 block would execute because both conditions are const age = 12;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
5. const age = 18;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
0 Nhà điều hành logic không [!] Và nếu ... các câu lệnh khác trong JavaScriptThe logical NOT [!] operator and if...else statements in JavaScript
Toán tử không [
const age = 17;
const ownsCar = true;
if [age >= 16 && ownsCar] {
console.log["Jerry is old enough to drive and has his own car."];
} else {
console.log["Jerry does not drive."];
}
7] hợp lý sẽ lấy một cái gì đó là const age = 12;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
4 và làm cho nó ____25. Nó cũng sẽ lấy một cái gì đó là const age = 12;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
5 và làm cho nó ____24.Chúng tôi có thể sửa đổi ví dụ từ trước đó để sử dụng toán tử
const age = 17;
const ownsCar = true;
if [age >= 16 && ownsCar] {
console.log["Jerry is old enough to drive and has his own car."];
} else {
console.log["Jerry does not drive."];
}
7 để tạo biến const age = 17;
const ownsCar = true;
if [age >= 16 && ownsCar] {
console.log["Jerry is old enough to drive and has his own car."];
} else {
console.log["Jerry does not drive."];
}
1 & nbsp; ________ 25. Vì cả hai điều kiện là const age = 12;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
5, khối const age = 12;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
1 sẽ được thực thi.Nhà điều hành const age = 18;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
1 điều kiện [ternary] trong JavaScriptConditional [ternary] operator in JavaScript
Nếu bạn có một câu lệnh
const age = 18;
if [age < 18] {
console.log["Alice is under 18 years old."];
} else if [age >= 18 && age = 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
2Điều kiện đi trước dấu
const age = 13;
const ownsCar = true;
if [age >= 16 && ownsCar] {
console.log["Jerry is old enough to drive and has his own car."];
} else {
console.log["Jerry does not drive."];
}
8 và nếu đó là const age = 12;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
4, thì mã giữa dấu const age = 13;
const ownsCar = true;
if [age >= 16 && ownsCar] {
console.log["Jerry is old enough to drive and has his own car."];
} else {
console.log["Jerry does not drive."];
}
8 và const boyfriendIsPaying = true;
const isSale = false;
if [boyfriendIsPaying || isSale] {
console.log["Jesse will go shopping."];
} else {
console.log["Jesse will not go shopping."];
}
1 sẽ thực thi. Nếu điều kiện là const age = 12;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
5, thì mã sau & nbsp; ________ 91 sẽ thực thi.Trong ví dụ này, vì tuổi lớn hơn 18, thì thông điệp đến giao diện điều khiển sẽ là "có thể bỏ phiếu".
const age = 18;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
3 Đây là những gì mã sẽ trông như thế nào khi sử dụng câu lệnh const age = 18;
if [age < 18] {
console.log["Alice is under 18 years old."];
} else if [age >= 18 && age = 18 && age = 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
4Sự kết luận
Các câu lệnh
const age = 18;
if [age < 18] {
console.log["Alice is under 18 years old."];
} else if [age >= 18 && age = 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
8 là const age = 18;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
9. Nếu điều kiện là const age = 12;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
0, thì khối const age = 12;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
1 sẽ được thực thi.Sẽ có những lúc bạn muốn kiểm tra nhiều điều kiện và bạn có thể sử dụng câu lệnh
const age = 18;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
00.Nếu bạn cảm thấy như câu lệnh
const age = 18;
if [age < 18] {
console.log["Alice is under 18 years old."];
} else if [age >= 18 && age = 18 && age = 18 && age = 18 && age