Language/Typescript

[Typescript ] Nullish Coalescing ( || vs ?? )

khakhalog 2023. 7. 25. 17:04

|| 와 ?? 차이

A || B

  • falsy()인 값에 대해 default 대체하는 연산자.
  • "", null, undefined, NaN, false, 0

A ? B

  • null 또는 undefined에 대해서만 default값으로 대체하는 연산자.
let x = foo ?? bar(); // 아래와 같은 맥락으로 쓰인다.

let x = foo !== null && foo !== undefined ? foo : bar();