Trong khi viết chương trình, có thể xảy ra tình huống bạn cần áp dụng một trong số các đường dẫn đã cho. Trong những trường hợp như vậy, bạn cần sử dụng các câu điều kiện cho phép chương trình của bạn đưa ra quyết định đúng và thực hiện đúng hành động
JavaScript hỗ trợ các câu điều kiện được sử dụng để thực hiện các hành động khác nhau dựa trên các điều kiện khác nhau. Ở đây chúng tôi sẽ giải thích nếu. tuyên bố khác
Lưu đồ của if-else
Lưu đồ sau đây cho thấy cách thức hoạt động của câu lệnh if-else
JavaScript hỗ trợ các dạng if sau. tuyên bố khác -
câu lệnh if
nếu. tuyên bố khác
nếu. khác nếu. bản tường trình
câu lệnh if
Câu lệnh if là câu lệnh điều khiển cơ bản cho phép JavaScript đưa ra quyết định và thực thi các câu lệnh có điều kiện
cú pháp
Cú pháp của câu lệnh if cơ bản như sau -
if [expression] { Statement[s] to be executed if expression is true }
Ở đây một biểu thức JavaScript được đánh giá. Nếu giá trị kết quả là đúng, [các] câu lệnh đã cho được thực thi. Nếu biểu thức sai thì không câu lệnh nào không được thực hiện. Hầu hết thời gian, bạn sẽ sử dụng các toán tử so sánh trong khi đưa ra quyết định
Ví dụ
Hãy thử ví dụ sau để hiểu cách hoạt động của câu lệnh if
Set the variable to different value and then try...
đầu ra
Qualifies for driving Set the variable to different value and then try...
nếu. tuyên bố khác
'nếu. câu lệnh khác là dạng câu lệnh điều khiển tiếp theo cho phép JavaScript thực thi các câu lệnh theo cách được kiểm soát nhiều hơn
cú pháp
if [expression] { Statement[s] to be executed if expression is true } else { Statement[s] to be executed if expression is false }
Ở đây biểu thức JavaScript được đánh giá. Nếu giá trị kết quả là đúng, [các] câu lệnh đã cho trong khối 'if', sẽ được thực thi. Nếu biểu thức là sai, thì [các] câu lệnh đã cho trong khối khác sẽ được thực thi
Ví dụ
Hãy thử đoạn mã sau để tìm hiểu cách triển khai câu lệnh if-else trong JavaScript
Set the variable to different value and then try...
đầu ra
Does not qualify for driving Set the variable to different value and then try...
nếu. khác nếu. bản tường trình
nếu. khác nếu. câu lệnh là một dạng nâng cao của if…else cho phép JavaScript đưa ra quyết định đúng trong một số điều kiện
cú pháp
Cú pháp của câu lệnh if-else-if như sau:
if [expression 1] { Statement[s] to be executed if expression 1 is true } else if [expression 2] { Statement[s] to be executed if expression 2 is true } else if [expression 3] { Statement[s] to be executed if expression 3 is true } else { Statement[s] to be executed if no expression is true }
Không có gì đặc biệt về mã này. Nó chỉ là một loạt các câu lệnh if, trong đó mỗi câu lệnh if là một phần của mệnh đề khác của câu lệnh trước đó. [Các] câu lệnh được thực thi dựa trên điều kiện đúng, nếu không có điều kiện nào đúng thì khối lệnh khác được thực thi
Sẽ có lúc bạn muốn viết các lệnh xử lý các quyết định khác nhau trong mã của mình
Ví dụ: nếu bạn đang mã hóa một bot, bạn có thể yêu cầu nó phản hồi bằng các thông báo khác nhau dựa trên một tập hợp các lệnh mà nó nhận được
Trong bài viết này, tôi sẽ giải thích câu lệnh
const age = 18;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
5 là gì và cung cấp các ví dụ mã. Chúng ta cũng sẽ xem xét toán tử có đ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."];
}
5một nếu là gì. tuyên bố khác trong JavaScript?
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 đ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à 20 thì khốiSet the variable to different value and then try...
21 sẽ được thực hiệnSet the variable to different value and then try...
Giá trị
22 vàSet the variable to different value and then try...
20 được chuyển đổi thànhSet the variable to different value and then try...
24 hoặcSet the variable to different value and then try...
25 trong câu lệnhSet the variable to different value and then try...
const age = 18;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
89Set the variable to different value and then try...
Bất kỳ giá trị nào không được xác định là
20 sẽ được coi làSet the variable to different value and then try...
const age = 18;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
9 trong JavaScriptĐây là danh sách các giá trị
20Set the variable to different value and then try...
- sai
- 0 [không]
- -0 [số 0 âm]
- 0n [BigInt không]
Qualifies for driving Set the variable to different value and then try...
30,Qualifies for driving Set the variable to different value and then try...
31,Qualifies for driving Set the variable to different value and then try...
32 [chuỗi trống]- vô giá trị
- không xác định
- NaN [không phải 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à 24 nên thông báo được in ra bảng điều khiển sẽ là "Nick là người lớn. "Set the variable to different value and then try...
const age = 18;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
Nhưng nếu tôi thay đổi biến
Qualifies for driving Set the variable to different value and then try...35 nhỏ hơn 18, thì điều kiện sẽ là
25 và thay vào đó, mã sẽ thực thi khốiSet the variable to different value and then try...
21.Set the variable to different value and then try...
2Set the variable to different value and then try...
Ví dụ về nhiều điều kiện [nếu. khác nếu. câu lệnh khác] trong JavaScript
Sẽ có lúc bạn muốn kiểm tra nhiều điều kiện. Đó là nơi mà khối
Qualifies for driving Set the variable to different value and then try...38 xuất hiện
Qualifies for driving Set the variable to different value and then try...3
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à 25, máy tính sẽ chuyển sang câu lệnhSet the variable to different value and then try...
Qualifies for driving Set the variable to different value and then try...38. Nếu đó cũng là
25, thì nó sẽ chuyển sang khốiSet the variable to different value and then try...
21Set the variable to different value and then try...
Trong ví dụ này, khối
Qualifies for driving Set the variable to different value and then try...38 sẽ được thực thi vì Alice ở độ tuổi từ 18 đến 21
1Set the variable to different value and then try...
Khi nào thì sử dụng câu lệnh switch over if. tuyên bố khác?
Đôi khi trong JavaScript, bạn có thể cân nhắc sử dụng câu lệnh
15 thay vì câu lệnhSet the variable to different value and then try...
16Set the variable to different value and then try...
Các câu lệnh
15 có thể có cú pháp rõ ràng hơn các câu lệnhSet the variable to different value and then try...
16 phức tạpSet the variable to different value and then try...
Hãy xem ví dụ bên dưới – thay vì sử dụng câu lệnh
16 dài này, bạn có thể chọn sử dụng câu lệnhSet the variable to different value and then try...
15 dễ đọc hơnSet the variable to different value and then try...
8Set the variable to different value and then try...
9Set the variable to different value and then try...
Câu lệnh
15 sẽ không phù hợp để sử dụng trong mọi tình huống. Nhưng nếu bạn cảm thấy câu lệnhSet the variable to different value and then try...
16 dài và phức tạp, thì câu lệnhSet the variable to different value and then try...
15 có thể là một lựa chọn thay thếSet the variable to different value and then try...
Toán tử logic AND [&&] và nếu. câu lệnh khác trong JavaScript
Trong toán tử logic AND [
84], nếu cả hai điều kiện đều làSet the variable to different value and then try...
24 thì khối lệnhSet the variable to different value and then try...
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à 25, thì khốiSet the variable to different value and then try...
21 sẽ được thực thiSet the variable to different value and then try...
Trong ví dụ này, vì tuổi lớn hơn 16 và biến
89 làSet the variable to different value and then try...
24, nên khốiSet the variable to different value and then try...
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 báo được in ra bảng điều khiển sẽ là "Jerry đủ tuổi lái xe và có ô tô riêng. "if [expression 1] { Statement[s] to be executed if expression 1 is true } else if [expression 2] { Statement[s] to be executed if expression 2 is true } else if [expression 3] { Statement[s] to be executed if expression 3 is true } else { Statement[s] to be executed if no expression is true }1
Nếu tôi thay đổi biến
Qualifies for driving Set the variable to different value and then try...35 thành nhỏ hơn 16, thì cả hai điều kiện không còn là
24 nữa và thay vào đó, khốiSet the variable to different value and then try...
21 sẽ được thực thi.Set the variable to different value and then try...
if [expression 1] { Statement[s] to be executed if expression 1 is true } else if [expression 2] { Statement[s] to be executed if expression 2 is true } else if [expression 3] { Statement[s] to be executed if expression 3 is true } else { Statement[s] to be executed if no expression is true }5
Định lý OR [. ] toán tử và nếu. câu lệnh khác trong JavaScript
Trong toán tử logic OR [
95], nếu một hoặc cả hai điều kiện làSet the variable to different value and then try...
24, thì mã bên trong câu lệnhSet the variable to different value and then try...
const age = 18;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
8 sẽ thực thiTrong ví dụ này, mặc dù biến
98 được đặt thànhSet the variable to different value and then try...
25, mã bên trong khốiSet the variable to different value and then try...
const age = 18;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
8 sẽ vẫn thực thi vì biến if [expression 1] { Statement[s] to be executed if expression 1 is true } else if [expression 2] { Statement[s] to be executed if expression 2 is true } else if [expression 3] { Statement[s] to be executed if expression 3 is true } else { Statement[s] to be executed if no expression is true }11 được đặt thành
24Set the variable to different value and then try...
const age = 18;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
4Nếu tôi thay đổi giá trị của biến
if [expression 1] { Statement[s] to be executed if expression 1 is true } else if [expression 2] { Statement[s] to be executed if expression 2 is true } else if [expression 3] { Statement[s] to be executed if expression 3 is true } else { Statement[s] to be executed if no expression is true }11 thành
25, thì khốiSet the variable to different value and then try...
21 sẽ thực thi vì cả hai điều kiện đều làSet the variable to different value and then try...
25.Set the variable to different value and then try...
const age = 18;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
0Đoạn logic NOT [. ] toán tử và nếu. câu lệnh khác trong JavaScript
Toán tử logic NOT [
if [expression 1] { Statement[s] to be executed if expression 1 is true } else if [expression 2] { Statement[s] to be executed if expression 2 is true } else if [expression 3] { Statement[s] to be executed if expression 3 is true } else { Statement[s] to be executed if no expression is true }17] sẽ lấy thứ gì đó là
24 và biến nó thànhSet the variable to different value and then try...
25. Nó cũng sẽ lấy thứ gì đó làSet the variable to different value and then try...
25 và biến nó thànhSet the variable to different value and then try...
24Set the variable to different value and then try...
Chúng ta có thể sửa đổi ví dụ trước đó để sử dụng toán tử
if [expression 1] { Statement[s] to be executed if expression 1 is true } else if [expression 2] { Statement[s] to be executed if expression 2 is true } else if [expression 3] { Statement[s] to be executed if expression 3 is true } else { Statement[s] to be executed if no expression is true }17 để tạo biến
if [expression 1] { Statement[s] to be executed if expression 1 is true } else if [expression 2] { Statement[s] to be executed if expression 2 is true } else if [expression 3] { Statement[s] to be executed if expression 3 is true } else { Statement[s] to be executed if no expression is true }11
25. Vì cả hai điều kiện đều làSet the variable to different value and then try...
25, nên khốiSet the variable to different value and then try...
21 sẽ được thực thiSet the variable to different value and then try...
const age = 18;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
1Toán tử [bộ ba] có điều kiện trong JavaScript
Nếu bạn có một câu lệnh
16 ngắn, thì bạn có thể chọn sử dụng toán tử bậc ba. Từ ternary có nghĩa là một cái gì đó bao gồm ba phầnSet the variable to different value and then try...
Đây là cú pháp cơ bản cho toán tử bậc ba
const age = 18;
if [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
if [expression 1] { Statement[s] to be executed if expression 1 is true } else if [expression 2] { Statement[s] to be executed if expression 2 is true } else if [expression 3] { Statement[s] to be executed if expression 3 is true } else { Statement[s] to be executed if no expression is true }58 và nếu nó là
24, thì mã giữa dấuSet the variable to different value and then try...
if [expression 1] { Statement[s] to be executed if expression 1 is true } else if [expression 2] { Statement[s] to be executed if expression 2 is true } else if [expression 3] { Statement[s] to be executed if expression 3 is true } else { Statement[s] to be executed if no expression is true }58 và
const age = 18;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
41 sẽ thực thi. Nếu điều kiện là 25, thì mã sau ______741 sẽ thực thiSet the variable to different value and then try...
Trong ví dụ này, vì tuổi lớn hơn 18 nên thông báo tới bảng đ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à giao diện của mã khi sử dụng câu lệnh
16.Set the variable to different value and then try...
const age = 18;
if [age >= 18] {
console.log["Nick is an adult."];
} else {
console.log["Nick is a child."];
}
4Phần kết luận
Câu lệnh
16 sẽ thực thi một khối mã khi điều kiện trong câu lệnhSet the variable to different value and then try...
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à 20 thì khốiSet the variable to different value and then try...
21 sẽ được thực hiệnSet the variable to different value and then try...
Sẽ có 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."];
}
00Nếu bạn cảm thấy câu lệnh
16 dài và phức tạp, thì câu lệnhSet the variable to different value and then try...
15 có thể là một lựa chọn thay thếSet the variable to different value and then try...
Sử dụng các toán tử logic để kiểm tra nhiều điều kiện có thể thay thế các câu lệnh
16 lồng nhauSet the variable to different value and then try...
Toán tử bậc ba có thể được sử dụng để viết mã ngắn hơn cho câu lệnh
16 đơn giảnSet the variable to different value and then try...
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
Tôi là một nhạc sĩ và một lập trình viên
Nếu bạn đọc đến đây, hãy tweet cho tác giả để cho họ thấy bạn quan tâm. Tweet một lời cảm ơn
Học cách viết mã miễn phí. Chương trình giảng dạy mã nguồn mở của freeCodeCamp đã giúp hơn 40.000 người có được việc làm với tư cách là nhà phát triển. Bắt đầu