ECMAScript 2020 introduced two new operators:
- Nullish coalescing operator (??). This operator returns the first operand if it is not null or undefined, and the second operand otherwise. For example, the expression "foo" ?? 123 will return the string "foo" if it is not null or undefined, and 123 otherwise.
- Optional chaining operator (?.). This operator allows you to safely access a property on an object, even if the object is null or undefined. For example, the expression user?.name will return the value of the name property of the user object if the user object is defined and has a name property, and undefined otherwise.
Here are some examples of how to use these operators:
const foo = null;
const bar = undefined;
// Nullish coalescing operator
const value = foo ?? bar; // value will be undefined
// Optional chaining operator
const name = user?.name; // name will be undefined if user is undefined or null
These operators can be used to make your code more concise and readable, and to avoid errors caused by accessing properties on null or undefined objects.
In addition to these two new operators, ECMAScript 2020 also introduced a number of other new features, such as:
- BigInt primitive type for arbitrary-sized integers
- String matchAll() method to match all occurrences of a regular expression in a string
- Logical assignment operators (&&=, ||=, ??=) to assign the result of a logical operation to a variable
- Promise.allSettled() method to wait for all promises in an iterable to resolve or reject
- Dynamic import() statement to import a module at runtime
These new features can make your JavaScript code more powerful and expressive.
ECMAScript 2020 introduced two new operators:
- Nullish coalescing operator (??). This operator returns the first operand if it is not null or undefined, and the second operand otherwise. For example, the expression "foo" ?? 123 will return the string "foo" if it is not null or undefined, and 123 otherwise.
- Optional chaining operator (?.). This operator allows you to safely access a property on an object, even if the object is null or undefined. For example, the expression user?.name will return the value of the name property of the user object if the user object is defined and has a name property, and undefined otherwise.
Here are some examples of how to use these operators:
const foo = null;
const bar = undefined;
// Nullish coalescing operator
const value = foo ?? bar; // value will be undefined
// Optional chaining operator
const name = user?.name; // name will be undefined if user is undefined or null
These operators can be used to make your code more concise and readable, and to avoid errors caused by accessing properties on null or undefined objects.
In addition to these two new operators, ECMAScript 2020 also introduced a number of other new features, such as:
- BigInt primitive type for arbitrary-sized integers
- String matchAll() method to match all occurrences of a regular expression in a string
- Logical assignment operators (&&=, ||=, ??=) to assign the result of a logical operation to a variable
- Promise.allSettled() method to wait for all promises in an iterable to resolve or reject
- Dynamic import() statement to import a module at runtime
These new features can make your JavaScript code more powerful and expressive.
Bard may display inaccurate or offensive information that doesn’t represent Google’s views. Bard Privacy Notice