Hướng dẫn can you use and in an if statement javascript? - bạn có thể sử dụng và trong một câu lệnh if javascript không?

Hướng dẫn can you use and in an if statement javascript? - bạn có thể sử dụng và trong một câu lệnh if javascript không?

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ố

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ụ 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.

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)
  • if (condition 1 is true) {
       // code is executed
    } else if (condition 2 is true) {
      // code is executed
    } else {
       // code is executed
    }
    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)
  • 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ế.
Hướng dẫn can you use and in an if statement javascript? - bạn có thể sử dụng và trong một câu lệnh if javascript không?

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 JavaScript
Hướng dẫn can you use and in an if statement javascript? - bạn có thể sử dụng và trong một câu lệnh if javascript không?

Examples 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 <= 21) {
  console.log("Alice is between the ages of 18 and 21.");
} else {
  console.log("Alice is over 21 years old.");
}
khi sử dụng các câu lệnh chuyển đổi nếu ... câu lệnh khác?
Hướng dẫn can you use and in an if statement javascript? - bạn có thể sử dụng và trong một câu lệnh if javascript không?

When to use switch statements over if...else statements?

Có những lúc trong JavaScript nơi bạn có thể cân nhắc 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 <= 21) {
  console.log("Alice is between the ages of 18 and 21.");
} else {
  console.log("Alice is over 21 years old.");
}
5 thay vì câu lệnh
const age = 18;

if (age < 18) {
  console.log("Alice is under 18 years old.");
} else if (age >= 18 && age <= 21) {
  console.log("Alice is between the ages of 18 and 21.");
} else {
  console.log("Alice is over 21 years old.");
}
6.

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 <= 21) {
  console.log("Alice is between the ages of 18 and 21.");
} else {
  console.log("Alice is over 21 years old.");
}
5 có thể có cú pháp sạch hơn so với các câu
const age = 18;

if (age < 18) {
  console.log("Alice is under 18 years old.");
} else if (age >= 18 && age <= 21) {
  console.log("Alice is between the ages of 18 and 21.");
} else {
  console.log("Alice is over 21 years old.");
}
6 phức tạp.

Hãy xem ví dụ dưới đây - thay vì 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 <= 21) {
  console.log("Alice is between the ages of 18 and 21.");
} else {
  console.log("Alice is over 21 years old.");
}
6 dài này, bạn có thể chọn đi với câu lệnh
const age = 18;

if (age < 18) {
  console.log("Alice is under 18 years old.");
} else if (age >= 18 && age <= 21) {
  console.log("Alice is between the ages of 18 and 21.");
} else {
  console.log("Alice is over 21 years old.");
}
5 dễ đọc hơ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");
}
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;
}

Các tuyên bố

const age = 18;

if (age < 18) {
  console.log("Alice is under 18 years old.");
} else if (age >= 18 && age <= 21) {
  console.log("Alice is between the ages of 18 and 21.");
} else {
  console.log("Alice is over 21 years old.");
}
5 sẽ không phù hợp để sử dụng trong tất cả các tình huống. Nhưng nếu bạn cảm thấy như 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 <= 21) {
  console.log("Alice is between the ages of 18 and 21.");
} else {
  console.log("Alice is over 21 years old.");
}
6 dài và phức tạp, thì một tuyên bố
const age = 18;

if (age < 18) {
  console.log("Alice is under 18 years old.");
} else if (age >= 18 && age <= 21) {
  console.log("Alice is between the ages of 18 and 21.");
} else {
  console.log("Alice is over 21 years old.");
}
5 có thể là một lựa chọn thay thế.

Toán tử logic và (&&) và nếu ... các câu lệnh khác trong JavaScript

Trong toán tử logic và (

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");
}
4), nếu 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ì 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ế.
Hướng dẫn can you use and in an if statement javascript? - bạn có thể sử dụng và trong một câu lệnh if javascript không?

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 JavaScript
Hướng dẫn can you use and in an if statement javascript? - bạn có thể sử dụng và trong một câu lệnh if javascript không?

The 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.
Hướng dẫn can you use and in an if statement javascript? - bạn có thể sử dụng và trong một câu lệnh if javascript không?

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 JavaScript
Hướng dẫn can you use and in an if statement javascript? - bạn có thể sử dụng và trong một câu lệnh if javascript không?

The 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 JavaScript
Hướng dẫn can you use and in an if statement javascript? - bạn có thể sử dụng và trong một câu lệnh if javascript không?

Conditional (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 <= 21) {
  console.log("Alice is between the ages of 18 and 21.");
} else {
  console.log("Alice is over 21 years old.");
}
6 ngắn, thì bạn có thể chọn đi với toán tử ternary. & nbsp; từ ternary có nghĩa là một cái gì đó bao gồm ba phần.

Đây là cú pháp cơ bản cho toán tử ternary:

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

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 <= 21) {
  console.log("Alice is between the ages of 18 and 21.");
} else {
  console.log("Alice is over 21 years old.");
}
6:
Hướng dẫn can you use and in an if statement javascript? - bạn có thể sử dụng và trong một câu lệnh if javascript không?

This is what the code would look like using an

const age = 18;

if (age < 18) {
  console.log("Alice is under 18 years old.");
} else if (age >= 18 && age <= 21) {
  console.log("Alice is between the ages of 18 and 21.");
} else {
  console.log("Alice is over 21 years old.");
}
6 statement:

const age = 18;

if (age >= 18) {
  console.log("Nick is an adult.");
} else {
  console.log("Nick is a child.");
}
4

Sự 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 <= 21) {
  console.log("Alice is between the ages of 18 and 21.");
} else {
  console.log("Alice is over 21 years old.");
}
6 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.

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 <= 21) {
  console.log("Alice is between the ages of 18 and 21.");
} else {
  console.log("Alice is over 21 years old.");
}
6 dài và phức tạp, thì 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 <= 21) {
  console.log("Alice is between the ages of 18 and 21.");
} else {
  console.log("Alice is over 21 years old.");
}
5 có thể là một lựa chọn thay thế.

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

const age = 18;

if (age < 18) {
  console.log("Alice is under 18 years old.");
} else if (age >= 18 && age <= 21) {
  console.log("Alice is between the ages of 18 and 21.");
} else {
  console.log("Alice is over 21 years old.");
}
6 lồng nhau.

Toán tử ternary có thể được sử dụng để viết mã ngắn hơn cho câu lệnh

const age = 18;

if (age < 18) {
  console.log("Alice is under 18 years old.");
} else if (age >= 18 && age <= 21) {
  console.log("Alice is between the ages of 18 and 21.");
} else {
  console.log("Alice is over 21 years old.");
}
6 đơn giản.



Học mã miễn phí. Chương trình giảng dạy 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

Bạn có thể sử dụng && trong một câu lệnh if javascript không?

Toán tử logic và (&&) và nếu ... các câu lệnh khác trong JavaScript. Trong toán tử logic và (&&), nếu cả hai điều kiện là đúng, thì khối nếu khối sẽ được thực thi. Nếu một hoặc cả hai điều kiện là sai, thì khối khác sẽ được thực thi.if both conditions are true , then the if block will be executed. If one or both of the conditions are false , then the else block will be executed.

Làm thế nào để bạn sử dụng một và toán tử trong nếu?

Khi bạn kết hợp từng người trong số chúng với câu lệnh IF, họ đọc như thế này: và - = if (và (một cái gì đó là sự thật, một cái gì đó khác là đúng), giá trị nếu đúng, giá trị nếu sai) hoặc - = if (hoặc (một cái gì đólà đúng, một cái gì đó khác là đúng), giá trị nếu đúng, giá trị nếu sai)AND – =IF(AND(Something is True, Something else is True), Value if True, Value if False) OR – =IF(OR(Something is True, Something else is True), Value if True, Value if False)

Bạn có thể đặt hai điều kiện trong một câu lệnh if javascript không?

Chúng ta cũng có thể viết nhiều điều kiện bên trong một câu lệnh IF với sự trợ giúp của các toán tử logic && và ||.Các toán tử && sẽ đánh giá nếu một điều kiện và một điều kiện khác là đúng.Cả hai phải đúng trước khi mã trong khối mã sẽ thực thi. . The && operators will evaluate if one condition AND another is true. Both must be true before the code in the code block will execute.

Bạn có thể có 3 điều kiện trong một câu lệnh if javascript?

Toán tử có điều kiện (ternary) là toán tử JavaScript duy nhất có ba toán hạng: một điều kiện theo sau là dấu hỏi (?), Sau đóThực thi nếu điều kiện là giả.: a condition followed by a question mark ( ? ), then an expression to execute if the condition is truthy followed by a colon ( : ), and finally the expression to execute if the condition is falsy.