Important Operator
Nullish coalescing operator
-> The nullish coalescing (??) operator is 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.
## Syntaxt: leftExpr ?? rightExpr
Documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing#assigning_a_default_value_to_a_variable
###Nullish coalescing assignment (??=)
-> The nullish coalescing assignment (??=) operator, also known as the logical nullish assignment operator,
only evaluates the right operand and assigns to the left if the left operand is nullish (null or undefined).
## Syntaxt: x ??= y
-> Exmaple:
const a = { duration: 50 };
a.duration ??= 10;
console.log(a.duration);
// Expected output: 50
a.speed ??= 25;
console.log(a.speed);
// Expected output: 25
Documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_assignment
Optional chaining (.)
-> The optional chaining (?.) operator accesses an object's property or calls a function. If the object accessed or function called using this operator
is undefined or null, the expression short circuits and evaluates to undefined instead of throwing an error.
### Syntax:
obj.val?.prop
obj.val?.[expr]
obj.func?.(args)
Example:
const adventurer = {
name: 'Alice',
cat: {
name: 'Dinah',
},
};
const dogName = adventurer.dog?.name;
console.log(dogName);
// Expected output: undefined
console.log(adventurer.someNonExistentMethod?.());
// Expected output: undefined
### Description: The ?. operator is like the . chaining operator, except that instead of causing an error if a reference is nullish (null or undefined),
the expression short-circuits with a return value of undefined. When used with function calls, it returns undefined if the given function does not exist.
###
By using the ?. operator instead of just ., JavaScript knows to implicitly check to be sure obj.first is not null or undefined
before attempting to access obj.first.second. If obj.first is null or undefined, the expression automatically short-circuits, returning undefined.
const nestedProp = obj.first?.second;
Documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining
Ternary Operator
###Syntax: condition ? exprIfTrue : exprIfFalse
###Description: Besides false, possible falsy expressions are: null, NaN, 0, the empty string (""), and undefined.
If condition is any of these, the result of the conditional expression will be the result of executing the expression exprIfFalse.
### Conditional chains:
The ternary operator is right-associative, which means it can be "chained" in the following way,
similar to an if … else if … else if … else chain:
function example() {
return condition1 ? value1
: condition2 ? value2
: condition3 ? value3
: value4;
}
Documentations: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_operator


0comments
Post a Comment