DEV Community

yeonseong
yeonseong

Posted on

?? in Javascript -Nullish coalescing operator-

?? 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
Enter fullscreen mode Exit fullscreen mode

example in react

const [who, setWho] = useState(initWho ?? "all");
Enter fullscreen mode Exit fullscreen mode

reference

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing

Top comments (1)

Collapse
 
jiwonwoo profile image
jwoo

예외처리할때 이거 유용하게 써먹어야겠네요 감사합니다!