New ES Edition
Optional Chaining Operator
Instead of causing an error if a reference is nullish (null
or undefined
), the expression short-circuits with a return value of undefined
const adventurer = {
name: 'Alice',
cat: {
name: 'Dinah'
}
};
const dogName = adventurer.dog?.name;
console.log(dogName); // undefined
console.log(adventurer.dog.name)
// Uncaught TypeError: Cannot read property 'name' of undefined
Nullish Coalescing Operator
a logical operator that returns its right-hand side operand when its left-hand side operand is null
or undefined
, and otherwise returns its left-hand side operand. This can be contrasted with the logical OR (||
) operator, which returns the right-hand side operand if the left operand is any falsy value, not only null
or undefined
.
const foo = null ?? 'default string';
console.log(foo);
// expected output: "default string"
const foo2 = null || 'default string';
console.log(foo2);
// expected output: "default string"
const baz = 0 ?? 42;
console.log(baz);
// expected output: 0
const baz2 = 0 || 42;
console.log(baz2);
// expected output: 42
Globalthis
globalThis is the universal that was previously use in browser as (window. )
and in terminal as (global. )
type:module
put type:module inside package.json to use import, export syntax in node
Last updated
Was this helpful?