Post

?? 연산자와 || 연산자

??

??는 null 병합 연산자이다.

왼쪽 피연산자가 null이거나 undefined일 때 오른쪽 피연산자를 반환하고, 그렇지 않으면 왼쪽 피연산자를 반환한다.

1
2
3
const foo = null ?? 'default string'
console.log(foo)
// 'default string'

||

||는 왼쪽 피연산자가 참(true)이면 왼쪽 피연산자를 반환하고, 그렇지 않으면 오른쪽 피연산자를 반환한다.

피연산자가 false인 경우는 아래와 같다.

  • null
  • NaN
  • 0
  • ’’
  • undefined

?? 와 || 의 차이

|| 연산자는 왼쪽 피연산자 값이 0, '' 일 때 오른쪽 피연산자를 반환하지만

?? 연산자는 왼쪽 피연산자 값이 0, '' 일 때 왼쪽 피연산자를 반환한다!

1
2
3
4
5
6
7
const baz = 0 ?? 42
console.log(baz)
// 0

const qty = 0 || 42
console.log(qty)
// 42

참고사이트

Nullish coalescing operator
Logical OR (||)

This post is licensed under CC BY 4.0 by the author.