?? in javascipt
Nullish coalescing operatior(??) is return right operand when left operand is null
or undefined
. if left operand is not nullish it return left operand.
example basic
const foo = null ?? 'default string';
console.log(foo);
// expected output: "default string"
const bar = 0 ?? 42;
console.log(bar);
// expected output: 0
example in react
const [who, setWho] = useState(initWho ?? "all");
reference
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing
Top comments (1)
예외처리할때 이거 유용하게 써먹어야겠네요 감사합니다!