Hướng dẫn what is null coalescing operator in javascript? - toán tử liên kết null trong javascript là gì?

Toán tử kết hợp Nullish (??) là một toán tử logic trả về toán hạng bên phải của nó khi toán hạng bên tay trái của nó là null hoặc

let foo;

//  foo is never assigned any value so it is still undefined
const someDummyText = foo || 'Hello!';
0, và nếu không thì sẽ trả lại toán hạng bên trái của nó.nullish coalescing operator (??) is a logical operator that returns its right-hand side operand when its left-hand side operand is null or
let foo;

//  foo is never assigned any value so it is still undefined
const someDummyText = foo || 'Hello!';
0, and otherwise returns its left-hand side operand.

Điều này có thể được coi là một trường hợp đặc biệt của toán tử logic hoặc (____11), trả về toán hạng bên phải nếu toán hạng bên trái là bất kỳ giá trị giả nào, không chỉ null hoặc

let foo;

//  foo is never assigned any value so it is still undefined
const someDummyText = foo || 'Hello!';
0. Nói cách khác, nếu bạn sử dụng
let foo;

//  foo is never assigned any value so it is still undefined
const someDummyText = foo || 'Hello!';
1 để cung cấp một số giá trị mặc định cho một biến khác
let foo;

//  foo is never assigned any value so it is still undefined
const someDummyText = foo || 'Hello!';
5, bạn có thể gặp phải các hành vi không mong muốn nếu bạn coi một số giá trị giả là có thể sử dụng được (ví dụ:
let foo;

//  foo is never assigned any value so it is still undefined
const someDummyText = foo || 'Hello!';
6 hoặc
let foo;

//  foo is never assigned any value so it is still undefined
const someDummyText = foo || 'Hello!';
7). Xem dưới đây để biết thêm ví dụ.

Toán tử kết hợp nullish có toán tử thấp thứ năm, thấp hơn trực tiếp so với

let foo;

//  foo is never assigned any value so it is still undefined
const someDummyText = foo || 'Hello!';
1 và cao hơn so với toán tử có điều kiện (ternary).

Thử nó

Cú pháp

Ví dụ

Sử dụng nhà điều hành liên kết nullish

Trong ví dụ này, chúng tôi sẽ cung cấp các giá trị mặc định nhưng giữ các giá trị khác với null hoặc

let foo;

//  foo is never assigned any value so it is still undefined
const someDummyText = foo || 'Hello!';
0.

const nullValue = null;
const emptyText = ""; // falsy
const someNumber = 42;

const valA = nullValue ?? "default for A";
const valB = emptyText ?? "default for B";
const valC = someNumber ?? 0;

console.log(valA); // "default for A"
console.log(valB); // "" (as the empty string is not null or undefined)
console.log(valC); // 42

Gán giá trị mặc định cho một biến

Trước đó, khi người ta muốn gán giá trị mặc định cho một biến, một mẫu chung là sử dụng logic hoặc toán tử (

let foo;

//  foo is never assigned any value so it is still undefined
const someDummyText = foo || 'Hello!';
1):

let foo;

//  foo is never assigned any value so it is still undefined
const someDummyText = foo || 'Hello!';

Tuy nhiên, do

let foo;

//  foo is never assigned any value so it is still undefined
const someDummyText = foo || 'Hello!';
1 là một toán tử logic boolean, toán hạng bên trái đã bị ép buộc để đánh giá và bất kỳ giá trị giả nào (
let foo;

//  foo is never assigned any value so it is still undefined
const someDummyText = foo || 'Hello!';
7,
let foo;

//  foo is never assigned any value so it is still undefined
const someDummyText = foo || 'Hello!';
6,
const count = 0;
const text = "";

const qty = count || 42;
const message = text || "hi!";
console.log(qty);     // 42 and not 0
console.log(message); // "hi!" and not ""
5, null,
let foo;

//  foo is never assigned any value so it is still undefined
const someDummyText = foo || 'Hello!';
0) không được trả lại. Hành vi này có thể gây ra hậu quả bất ngờ nếu bạn coi
let foo;

//  foo is never assigned any value so it is still undefined
const someDummyText = foo || 'Hello!';
7,
let foo;

//  foo is never assigned any value so it is still undefined
const someDummyText = foo || 'Hello!';
6 hoặc
const count = 0;
const text = "";

const qty = count || 42;
const message = text || "hi!";
console.log(qty);     // 42 and not 0
console.log(message); // "hi!" and not ""
5 là giá trị hợp lệ.

const count = 0;
const text = "";

const qty = count || 42;
const message = text || "hi!";
console.log(qty);     // 42 and not 0
console.log(message); // "hi!" and not ""

Toán tử kết hợp nullish tránh được cạm bẫy này bằng cách chỉ trả lại toán hạng thứ hai khi cái đầu tiên đánh giá thành null hoặc

let foo;

//  foo is never assigned any value so it is still undefined
const someDummyText = foo || 'Hello!';
0 (nhưng không có giá trị giả nào khác):

const myText = ''; // An empty string (which is also a falsy value)

const notFalsyText = myText || 'Hello world';
console.log(notFalsyText); // Hello world

const preservingFalsy = myText ?? 'Hi neighborhood';
console.log(preservingFalsy); // '' (as myText is neither undefined nor null)

Short-circuiting

Giống như các toán tử OR và và logic, biểu thức bên phải không được đánh giá nếu phía bên trái chứng minh là không null cũng không

let foo;

//  foo is never assigned any value so it is still undefined
const someDummyText = foo || 'Hello!';
0.

function A() { console.log('A was called'); return undefined;}
function B() { console.log('B was called'); return false;}
function C() { console.log('C was called'); return "foo";}

console.log(A() ?? C());
// logs "A was called" then "C was called" and then "foo"
// as A() returned undefined so both expressions are evaluated

console.log(B() ?? C());
// logs "B was called" then "false"
// as B() returned false (and not null or undefined), the right
// hand side expression was not evaluated

Không có chuỗi với và hoặc các nhà khai thác

Không thể kết hợp cả và (

const myText = ''; // An empty string (which is also a falsy value)

const notFalsyText = myText || 'Hello world';
console.log(notFalsyText); // Hello world

const preservingFalsy = myText ?? 'Hi neighborhood';
console.log(preservingFalsy); // '' (as myText is neither undefined nor null)
5) và các toán tử (
let foo;

//  foo is never assigned any value so it is still undefined
const someDummyText = foo || 'Hello!';
1) trực tiếp với ??. Một
const myText = ''; // An empty string (which is also a falsy value)

const notFalsyText = myText || 'Hello world';
console.log(notFalsyText); // Hello world

const preservingFalsy = myText ?? 'Hi neighborhood';
console.log(preservingFalsy); // '' (as myText is neither undefined nor null)
8 sẽ được ném trong những trường hợp như vậy.

null || undefined ?? "foo"; // raises a SyntaxError
true || undefined ?? "foo"; // raises a SyntaxError

Tuy nhiên, việc cung cấp dấu ngoặc đơn để chỉ ra một cách rõ ràng ưu tiên là chính xác:

(null || undefined) ?? "foo"; // returns "foo"

Mối quan hệ với toán tử chuỗi tùy chọn (const myText = ''; // An empty string (which is also a falsy value) const notFalsyText = myText || 'Hello world'; console.log(notFalsyText); // Hello world const preservingFalsy = myText ?? 'Hi neighborhood'; console.log(preservingFalsy); // '' (as myText is neither undefined nor null) 9)

Toán tử kết hợp nullish coi ____10 và null là các giá trị cụ thể và toán tử chuỗi tùy chọn (

const myText = ''; // An empty string (which is also a falsy value)

const notFalsyText = myText || 'Hello world';
console.log(notFalsyText); // Hello world

const preservingFalsy = myText ?? 'Hi neighborhood';
console.log(preservingFalsy); // '' (as myText is neither undefined nor null)
9) cũng hữu ích để truy cập vào một thuộc tính của một đối tượng có thể là null hoặc
let foo;

//  foo is never assigned any value so it is still undefined
const someDummyText = foo || 'Hello!';
0.

const foo = { someFooProp: "hi" };

console.log(foo.someFooProp?.toUpperCase() ?? "not available"); // "HI"
console.log(foo.someBarProp?.toUpperCase() ?? "not available"); // "not available"

Thông số kỹ thuật

Sự chỉ rõ
Đặc tả ngôn ngữ Ecmascript # prod-coalescepression
# prod-CoalesceExpression

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

Việc sử dụng nhà điều hành Null Coalescing là gì?

Các toán tử khống không ??Trả về giá trị của toán hạng bên trái của nó nếu nó không phải là null;Mặt khác, nó đánh giá toán hạng bên phải và trả về kết quả của nó.Các ??Nhà điều hành không đánh giá toán hạng bên phải của nó nếu toán hạng bên trái đánh giá là không null.returns the value of its left-hand operand if it isn't null ; otherwise, it evaluates the right-hand operand and returns its result. The ?? operator doesn't evaluate its right-hand operand if the left-hand operand evaluates to non-null.

Có một nhà điều hành kết hợp null trong JavaScript?

JavaScript đảm bảo điều này có thể được xử lý với toán tử Nullish còn được gọi là toán tử Null Coalescing, được thêm vào ngôn ngữ với Ecmascript 2020. Với nó, bạn có thể trả lại giá trị hoặc gán nó cho một số giá trị khác, tùy thuộc vào Booleanbiểu hiện., which was added to the language with ECMAScript 2020. With it, you can either return a value or assign it to some other value, depending on a boolean expression.

2 dấu hỏi có nghĩa là gì trong JavaScript?

Toán tử đánh dấu câu hỏi kép JavaScript (??) được gọi là toán tử kết hợp nullish và nó cung cấp một giá trị mặc định khi một biến hoặc biểu thức đánh giá là null hoặc không xác định.provides a default value when a variable or an expression evaluates to null or undefined.

JavaScript nullish là gì?

Trong JavaScript, một giá trị vô giá trị là giá trị là null hoặc không xác định.Các giá trị nullish luôn là giả mạo.the value which is either null or undefined . Nullish values are always falsy.