Hướng dẫn javascript double question mark - dấu hỏi kép javascript

Khi bạn kiểm tra mã JavaScript, bạn có thể tìm thấy một biểu thức bằng dấu câu hỏi kép (

let firstName = null;
let username = firstName ?? "Guest";
console.log(username); // "Guest"
6) như trong mã dưới đây:

alert(username ?? "Guest");

Toán tử đánh dấu câu hỏi kép được gọi là toán tử kết hợp nullish và nó là một tính năng mới của JavaScript ES2020 cho phép bạn cung cấp giá trị mặc định để sử dụng khi biểu thức biến đánh giá thành

let firstName = null;
let username = firstName ?? "Guest";
console.log(username); // "Guest"
7 hoặc
let firstName = null;
let username = firstName ?? "Guest";
console.log(username); // "Guest"
8.

Toán tử kết hợp không có hình chữ nhật chỉ đơn giản là trả lại biểu thức ở phía bên phải của nhãn hiệu khi biểu thức ở phía bên trái là

let firstName = null;
let username = firstName ?? "Guest";
console.log(username); // "Guest"
7 hoặc
let firstName = null;
let username = firstName ?? "Guest";
console.log(username); // "Guest"
8.returns the expression on the right-side of the mark when the expression on the left side is
let firstName = null;
let username = firstName ?? "Guest";
console.log(username); // "Guest"
7 or
let firstName = null;
let username = firstName ?? "Guest";
console.log(username); // "Guest"
8.returns the expression on the right-side of the mark when the expression on the left side is
let firstName = null;
let username = firstName ?? "Guest";
console.log(username); // "Guest"
7 or
let firstName = null;
let username = firstName ?? "Guest";
console.log(username); // "Guest"
8.

Ví dụ: mã sau sẽ gán giá trị biến

let firstName = null;
let username = firstName ?? "Guest";
console.log(username); // "Guest"
1 làm giá trị của biến
let firstName = null;
let username = firstName ?? "Guest";
console.log(username); // "Guest"
2. Khi giá trị
let firstName = null;
let username = firstName ?? "Guest";
console.log(username); // "Guest"
1 là NULL,
let firstName = null;
let username = firstName ?? "Guest";
console.log(username); // "Guest"
2 sẽ được gán là
let firstName = null;
let username = firstName ?? "Guest";
console.log(username); // "Guest"
5:
let firstName = null;
let username = firstName ?? "Guest";
console.log(username); // "Guest"

Hoặc bạn cũng có thể viết nó theo cách này:

let username = undefined ?? "Guest";
console.log(username); // "Guest"

Không giống như toán tử JavaScript hoặc (

let firstName = null;
let username = firstName ?? "Guest";
console.log(username); // "Guest"
6), toán tử kết hợp vô hiệu hóa đã giành được các giá trị
let firstName = null;
let username = firstName ?? "Guest";
console.log(username); // "Guest"
7 với biểu thức bên phải. Một ví dụ về điều này là khi bạn vượt qua một chuỗi trống (
let firstName = null;
let username = firstName ?? "Guest";
console.log(username); // "Guest"
8) làm biểu thức bên trái.

JavaScript đánh giá một chuỗi trống thành

let firstName = null;
let username = firstName ?? "Guest";
console.log(username); // "Guest"
7 như trong mã bên dưới:
let firstName = null;
let username = firstName ?? "Guest";
console.log(username); // "Guest"
2

Toán tử kết hợp nullish đã được tạo ra vì sẽ có những lúc bạn muốn xem xét các giá trị giả tưởng như

let username = undefined ?? "Guest";
console.log(username); // "Guest"
0 và chuỗi trống
let firstName = null;
let username = firstName ?? "Guest";
console.log(username); // "Guest"
8 là giá trị hợp lệ. Việc sử dụng hoặc toán tử sẽ ngăn bạn trả lại bất kỳ giá trị giả nào.

Bằng cách sử dụng toán tử kết hợp nullish, bạn sẽ chỉ thay thế chính xác các giá trị

let firstName = null;
let username = firstName ?? "Guest";
console.log(username); // "Guest"
7 và
let firstName = null;
let username = firstName ?? "Guest";
console.log(username); // "Guest"
8 với giá trị xác định.exactly
let firstName = null;
let username = firstName ?? "Guest";
console.log(username); // "Guest"
7 and
let firstName = null;
let username = firstName ?? "Guest";
console.log(username); // "Guest"
8 values with a defined value.exactly
let firstName = null;
let username = firstName ?? "Guest";
console.log(username); // "Guest"
7 and
let firstName = null;
let username = firstName ?? "Guest";
console.log(username); // "Guest"
8 values with a defined value.

Sử dụng ?? với || và && nhà khai thác

Vì lý do an toàn, dấu hỏi kép có thể được sử dụng cùng với các toán tử JavaScript hoặc (

let firstName = null;
let username = firstName ?? "Guest";
console.log(username); // "Guest"
6) và (
let username = undefined ?? "Guest";
console.log(username); // "Guest"
5) mà không có dấu ngoặc đơn
let username = undefined ?? "Guest";
console.log(username); // "Guest"
6 ngăn cách các toán tử.

Ví dụ: mã sau thử xem có thể sử dụng biến

let firstName = null;
let username = firstName ?? "Guest";
console.log(username); // "Guest"
1 hay
let username = undefined ?? "Guest";
console.log(username); // "Guest"
8 làm giá trị của
let firstName = null;
let username = firstName ?? "Guest";
console.log(username); // "Guest"
2 trước khi sử dụng
let firstName = null;
let username = firstName ?? "Guest";
console.log(username); // "Guest"
5 làm giá trị của nó:
let firstName = null;
let username = firstName ?? "Guest";
console.log(username); // "Guest"
2

Điều này là do JavaScript won có thể xác định nhà điều hành nào cần đánh giá trước. Bạn cần sử dụng dấu ngoặc đơn để chỉ ra rõ ràng mức độ ưu tiên của các đánh giá.

Mã sau đây sẽ đánh giá các biểu thức bên trong dấu ngoặc đơn:

let firstName = null;
let username = firstName ?? "Guest";
console.log(username); // "Guest"
3

Và đó là cách mà bạn kết hợp người vận hành kết hợp không có một trong hai và hoặc hoặc nhà điều hành.

Sự kết luận

Dấu hỏi kép JavaScript còn được gọi là nhà điều hành kết hợp nullish. Nó có một toán tử chỉ đơn giản là trả về biểu thức bên phải khi biểu thức bên trái là

let firstName = null;
let username = firstName ?? "Guest";
console.log(username); // "Guest"
7 hoặc
let firstName = null;
let username = firstName ?? "Guest";
console.log(username); // "Guest"
8.

Không giống như hoặc toán tử, toán tử kết hợp nullish là một toán tử tình huống cho phép bạn trả về

let username = undefined ?? "Guest";
console.log(username); // "Guest"
0 và chuỗi trống
let firstName = null;
let username = firstName ?? "Guest";
console.log(username); // "Guest"
8 như một giá trị hợp lệ cho ứng dụng của bạn.

Don Tiết quên sử dụng dấu ngoặc đơn khi bạn kết hợp nó với và / hoặc nhà điều hành 😉

Dấu câu hỏi có nghĩa là gì trong JS?

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ả.an expression to execute if the condition is truthy followed by a colon ( : ), and finally the expression to execute if the condition is falsy.an expression to execute if the condition is truthy followed by a colon ( : ), and finally the expression to execute if the condition is falsy.

Việc sử dụng dấu câu hỏi kép trong TypeScript là gì?

Dấu hỏi?Trong TypeScript được sử dụng theo hai cách: đề cập rằng một biến cụ thể là tùy chọn.Để kiểm tra trước nếu một biến thành viên có mặt cho một đối tượng.To mention that a particular variable is optional. To pre-check if a member variable is present for an object.To mention that a particular variable is optional. To pre-check if a member variable is present for an object.

Làm thế nào để bạn làm hai dấu hỏi?

Nếu các dấu hỏi kép được sử dụng, rõ ràng là nhấn mạnh một cái gì đó, thường là từ cú sốc của điều trước đây đã nói, để thể hiện một cảm xúc mạnh mẽ.'Có thật không??'Vì vậy, để truyền đạt cảm xúc sốc đó, chúng tôi nhấn mạnh bằng cách sử dụng dấu hỏi kép.to emphasise something in return, usually from the shock of the previous thing said, to express a strong emotion. 'Really??' So, to convey that emotion of shock, we emphasise by using double question marks.to emphasise something in return, usually from the shock of the previous thing said, to express a strong emotion. 'Really??' So, to convey that emotion of shock, we emphasise by using double question marks.