Hướng dẫn ternary operator with boolean in javascript - toán tử ternary với boolean trong 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 câu hỏi (

const age = 26;
const beverage = age >= 21 ? "Beer" : "Juice";
console.log(beverage); // "Beer"
5), sau đó là một biểu thức để thực thi nếu điều kiện là sự thật theo sau là dấu hai chấm (
const age = 26;
const beverage = age >= 21 ? "Beer" : "Juice";
console.log(beverage); // "Beer"
6) và cuối cùng là biểu thức thành Thực thi nếu điều kiện là giả. Toán tử này thường được sử dụng thay thế cho câu lệnh
const age = 26;
const beverage = age >= 21 ? "Beer" : "Juice";
console.log(beverage); // "Beer"
7.conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark (
const age = 26;
const beverage = age >= 21 ? "Beer" : "Juice";
console.log(beverage); // "Beer"
5), then an expression to execute if the condition is truthy followed by a colon (
const age = 26;
const beverage = age >= 21 ? "Beer" : "Juice";
console.log(beverage); // "Beer"
6), and finally the expression to execute if the condition is falsy. This operator is frequently used as an alternative to an
const age = 26;
const beverage = age >= 21 ? "Beer" : "Juice";
console.log(beverage); // "Beer"
7 statement.

Thử nó

Cú pháp

condition ? exprIfTrue : exprIfFalse

Thông số

const age = 26;
const beverage = age >= 21 ? "Beer" : "Juice";
console.log(beverage); // "Beer"
8

Một biểu thức có giá trị được sử dụng làm điều kiện.

const age = 26;
const beverage = age >= 21 ? "Beer" : "Juice";
console.log(beverage); // "Beer"
9

Một biểu thức được thực thi nếu

const age = 26;
const beverage = age >= 21 ? "Beer" : "Juice";
console.log(beverage); // "Beer"
8 đánh giá thành giá trị sự thật (một biểu hiện bằng hoặc có thể được chuyển đổi thành
const greeting = (person) => {
  const name = person ? person.name : "stranger";
  return `Howdy, ${name}`;
}

console.log(greeting({ name: "Alice" }));  // "Howdy, Alice"
console.log(greeting(null));             // "Howdy, stranger"
1).

const greeting = (person) => {
  const name = person ? person.name : "stranger";
  return `Howdy, ${name}`;
}

console.log(greeting({ name: "Alice" }));  // "Howdy, Alice"
console.log(greeting(null));             // "Howdy, stranger"
2

Một biểu thức được thực thi nếu

const age = 26;
const beverage = age >= 21 ? "Beer" : "Juice";
console.log(beverage); // "Beer"
8 là giả (nghĩa là có giá trị có thể được chuyển đổi thành
const greeting = (person) => {
  const name = person ? person.name : "stranger";
  return `Howdy, ${name}`;
}

console.log(greeting({ name: "Alice" }));  // "Howdy, Alice"
console.log(greeting(null));             // "Howdy, stranger"
4).

Sự mô tả

Bên cạnh

const greeting = (person) => {
  const name = person ? person.name : "stranger";
  return `Howdy, ${name}`;
}

console.log(greeting({ name: "Alice" }));  // "Howdy, Alice"
console.log(greeting(null));             // "Howdy, stranger"
4, các biểu thức giả có thể là:
const greeting = (person) => {
  const name = person ? person.name : "stranger";
  return `Howdy, ${name}`;
}

console.log(greeting({ name: "Alice" }));  // "Howdy, Alice"
console.log(greeting(null));             // "Howdy, stranger"
6,
const greeting = (person) => {
  const name = person ? person.name : "stranger";
  return `Howdy, ${name}`;
}

console.log(greeting({ name: "Alice" }));  // "Howdy, Alice"
console.log(greeting(null));             // "Howdy, stranger"
7,
const greeting = (person) => {
  const name = person ? person.name : "stranger";
  return `Howdy, ${name}`;
}

console.log(greeting({ name: "Alice" }));  // "Howdy, Alice"
console.log(greeting(null));             // "Howdy, stranger"
8, chuỗi trống (
const greeting = (person) => {
  const name = person ? person.name : "stranger";
  return `Howdy, ${name}`;
}

console.log(greeting({ name: "Alice" }));  // "Howdy, Alice"
console.log(greeting(null));             // "Howdy, stranger"
9) và
function example() {
  return condition1 ? value1
        : condition2 ? value2
        : condition3 ? value3
        : value4;
}
0. Nếu
const age = 26;
const beverage = age >= 21 ? "Beer" : "Juice";
console.log(beverage); // "Beer"
8 là bất kỳ trong số này, kết quả của biểu thức có điều kiện sẽ là kết quả của việc thực hiện biểu thức
const greeting = (person) => {
  const name = person ? person.name : "stranger";
  return `Howdy, ${name}`;
}

console.log(greeting({ name: "Alice" }));  // "Howdy, Alice"
console.log(greeting(null));             // "Howdy, stranger"
2.

Ví dụ

Một ví dụ đơn giản

const age = 26;
const beverage = age >= 21 ? "Beer" : "Juice";
console.log(beverage); // "Beer"

Xử lý các giá trị null

Một cách sử dụng phổ biến là xử lý một giá trị có thể là

const greeting = (person) => {
  const name = person ? person.name : "stranger";
  return `Howdy, ${name}`;
}

console.log(greeting({ name: "Alice" }));  // "Howdy, Alice"
console.log(greeting(null));             // "Howdy, stranger"
6:

const greeting = (person) => {
  const name = person ? person.name : "stranger";
  return `Howdy, ${name}`;
}

console.log(greeting({ name: "Alice" }));  // "Howdy, Alice"
console.log(greeting(null));             // "Howdy, stranger"

Chuỗi có điều kiện

Toán tử ternary là liên kết đúng, có nghĩa là nó có thể được "xích" theo cách sau, tương tự như chuỗi

function example() {
  return condition1 ? value1
        : condition2 ? value2
        : condition3 ? value3
        : value4;
}
4:

function example() {
  return condition1 ? value1
        : condition2 ? value2
        : condition3 ? value3
        : value4;
}

Điều này tương đương với chuỗi

const age = 26;
const beverage = age >= 21 ? "Beer" : "Juice";
console.log(beverage); // "Beer"
7 sau đây.

function example() {
  if (condition1) {
    return value1;
  } else if (condition2) {
    return value2;
  } else if (condition3) {
    return value3;
  } else {
    return value4;
  }
}

Thông số kỹ thuật

Sự chỉ rõ
Đặc tả ngôn ngữ Ecmascript # Sec-Contressal-Trình vận hành
# sec-conditional-operator

Tính tương thích của trình duyệt web

Bảng BCD chỉ tải trong trình duyệt

Xem thêm

Hướng dẫn ternary operator with boolean in javascript - toán tử ternary với boolean trong javascript

Tóm tắt: Trong hướng dẫn này, bạn sẽ tìm hiểu cách sử dụng toán tử ternary JavaScript để làm cho mã của bạn súc tích hơn. & NBSP;: in this tutorial, you will learn how to use the JavaScript ternary operator to make your code more concise. 

Giới thiệu về toán tử ternary JavaScript

Khi bạn muốn thực thi một khối nếu một điều kiện đánh giá thành

const greeting = (person) => {
  const name = person ? person.name : "stranger";
  return `Howdy, ${name}`;
}

console.log(greeting({ name: "Alice" }));  // "Howdy, Alice"
console.log(greeting(null));             // "Howdy, stranger"
1, bạn thường sử dụng một câu lệnh IF khác. Ví dụ:

let age = 18; let message; if (age >= 16) { message = 'You can drive.'; } else { message = 'You cannot drive.'; } console.log(message);

Code language: JavaScript (javascript)

Trong ví dụ này, chúng tôi hiển thị một thông điệp rằng một người có thể lái xe nếu tuổi lớn hơn hoặc bằng 16. Ngoài ra, bạn có thể sử dụng toán tử ternary thay vì câu lệnh if-else như thế này:

let age = 18; let message; age >= 16 ? (message = 'You can drive.') : (message = 'You cannot drive.'); console.log(message);

Code language: JavaScript (javascript)

Hoặc bạn có thể sử dụng toán tử ternary trong một biểu thức như sau:

let age = 18; let message; message = age >= 16 ? 'You can drive.' : 'You cannot drive.'; console.log(message);

Code language: JavaScript (javascript)

Tại đây, cú pháp của toán tử ternary:

condition ? expressionIfTrue : expressionIfFalse;

Code language: JavaScript (javascript)

Trong cú pháp này,

const age = 26;
const beverage = age >= 21 ? "Beer" : "Juice";
console.log(beverage); // "Beer"
8 là một biểu thức đánh giá theo giá trị boolean, hoặc
const greeting = (person) => {
  const name = person ? person.name : "stranger";
  return `Howdy, ${name}`;
}

console.log(greeting({ name: "Alice" }));  // "Howdy, Alice"
console.log(greeting(null));             // "Howdy, stranger"
1 hoặc
const greeting = (person) => {
  const name = person ? person.name : "stranger";
  return `Howdy, ${name}`;
}

console.log(greeting({ name: "Alice" }));  // "Howdy, Alice"
console.log(greeting(null));             // "Howdy, stranger"
4.

Nếu điều kiện là

const greeting = (person) => {
  const name = person ? person.name : "stranger";
  return `Howdy, ${name}`;
}

console.log(greeting({ name: "Alice" }));  // "Howdy, Alice"
console.log(greeting(null));             // "Howdy, stranger"
1, biểu thức đầu tiên (
function example() {
  if (condition1) {
    return value1;
  } else if (condition2) {
    return value2;
  } else if (condition3) {
    return value3;
  } else {
    return value4;
  }
}
1) sẽ thực thi. Nếu nó là sai, biểu thức thứ hai (
function example() {
  if (condition1) {
    return value1;
  } else if (condition2) {
    return value2;
  } else if (condition3) {
    return value3;
  } else {
    return value4;
  }
}
2) sẽ thực thi.

Sau đây cho thấy cú pháp của toán tử ternary được sử dụng trong một biểu thức:

let variableName = condition ? expressionIfTrue : expressionIfFalse;

Code language: JavaScript (javascript)

Trong cú pháp này, nếu

const age = 26;
const beverage = age >= 21 ? "Beer" : "Juice";
console.log(beverage); // "Beer"
8 là
const greeting = (person) => {
  const name = person ? person.name : "stranger";
  return `Howdy, ${name}`;
}

console.log(greeting({ name: "Alice" }));  // "Howdy, Alice"
console.log(greeting(null));             // "Howdy, stranger"
1,
function example() {
  if (condition1) {
    return value1;
  } else if (condition2) {
    return value2;
  } else if (condition3) {
    return value3;
  } else {
    return value4;
  }
}
5 sẽ lấy kết quả của biểu thức đầu tiên (
function example() {
  if (condition1) {
    return value1;
  } else if (condition2) {
    return value2;
  } else if (condition3) {
    return value3;
  } else {
    return value4;
  }
}
6) hoặc
function example() {
  if (condition1) {
    return value1;
  } else if (condition2) {
    return value2;
  } else if (condition3) {
    return value3;
  } else {
    return value4;
  }
}
2 khác.

Hãy cùng lấy một số ví dụ về việc sử dụng toán tử ternary.

1) Sử dụng toán tử ternary JavaScript để thực hiện nhiều câu lệnh

Ví dụ sau sử dụng toán tử ternary để thực hiện nhiều thao tác, trong đó mỗi thao tác được phân tách bằng dấu phẩy. Ví dụ:

const age = 26;
const beverage = age >= 21 ? "Beer" : "Juice";
console.log(beverage); // "Beer"
0

Trong ví dụ này, giá trị trả về của toán tử ternary là giá trị cuối cùng trong danh sách được phân tách bằng dấu phẩy.

2) đơn giản hóa ví dụ về toán tử ternary

Xem ví dụ sau:

const age = 26;
const beverage = age >= 21 ? "Beer" : "Juice";
console.log(beverage); // "Beer"
1

Nếu

function example() {
  if (condition1) {
    return value1;
  } else if (condition2) {
    return value2;
  } else if (condition3) {
    return value3;
  } else {
    return value4;
  }
}
8 là 1, thì biến
function example() {
  if (condition1) {
    return value1;
  } else if (condition2) {
    return value2;
  } else if (condition3) {
    return value3;
  } else {
    return value4;
  }
}
9 được đặt thành
const greeting = (person) => {
  const name = person ? person.name : "stranger";
  return `Howdy, ${name}`;
}

console.log(greeting({ name: "Alice" }));  // "Howdy, Alice"
console.log(greeting(null));             // "Howdy, stranger"
4, nếu không, đó là & nbsp; set & nbsp; thành
const greeting = (person) => {
  const name = person ? person.name : "stranger";
  return `Howdy, ${name}`;
}

console.log(greeting({ name: "Alice" }));  // "Howdy, Alice"
console.log(greeting(null));             // "Howdy, stranger"
1. Trong trường hợp này, bạn có thể đơn giản hóa nó bằng cách sử dụng biểu thức boolean như sau:

const age = 26;
const beverage = age >= 21 ? "Beer" : "Juice";
console.log(beverage); // "Beer"
2

3) Sử dụng nhiều toán tử ternary JavaScript

Ví dụ sau đây cho thấy cách sử dụng hai toán tử ternary trong cùng một biểu thức:

const age = 26;
const beverage = age >= 21 ? "Beer" : "Juice";
console.log(beverage); // "Beer"
3

Output:

const age = 26;
const beverage = age >= 21 ? "Beer" : "Juice";
console.log(beverage); // "Beer"
4

Nó có một thực hành tốt để sử dụng toán tử ternary khi nó làm cho mã dễ đọc hơn. Nếu logic chứa nhiều câu lệnh

const age = 26;
const beverage = age >= 21 ? "Beer" : "Juice";
console.log(beverage); // "Beer"
7, bạn nên tránh sử dụng các toán tử ternary.

Bản tóm tắt

  • Sử dụng toán tử ternary JavaScript (____ 53) để làm cho mã ngắn gọn hơn.

Hướng dẫn này có hữu ích không?

JavaScript có hỗ trợ toán tử ternary đúng hay sai?

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ả. that takes three operands: 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.

Chúng ta có thể sử dụng toán tử ternary bên trong toán tử ternary trong JavaScript không?

Ví dụ: bạn không nên cố gắng làm cho các toán tử ternary lồng nhau bên trong các toán tử ternary.Mặc dù viết mã theo cách này hoạt động chính xác trong JavaScript, nhưng rất khó để đọc và hiểu.you should not try to make ternary operators nested inside of ternary operators. Although writing code this way works correctly in JavaScript, it is very hard to read and understand.

Chúng ta có thể sử dụng câu lệnh trả về trong nhà điều hành ternary JavaScript không?

Bạn không thể gán câu lệnh trả về cho một biến.Nếu bạn muốn hoạt động được gán giá trị đúng hoặc sai, chỉ cần xóa trả về s: var action = sort.. If you want active to be assigned the value true or false , just delete the return s: var active = sort.

Toán tử Ternary có nhanh hơn nếu JavaScript không?

Dưới đây là sự khác biệt chính giữa các toán tử ternary và các khối if-else: toán tử ternary là một câu lệnh duy nhất, trong khi IF-Else là một khối mã.Một toán tử ternary nhanh hơn một khối if-else.A ternary operator is faster than an if-else block.