짧은 문법으로 여러 피연산자 중 그 값이 ‘확정되어있는’ 변수를 찾을 수 있음
a ?? b
a
가 null
도 아니고 undefined
도 아니면 a
b
<script>
let firstName = null;
let lastName = null;
let nickName = '한승연';
alert(firstName ?? lastName ?? nickName ?? '익명의 사용자'); //한승연
</script>
→ 값이 정해진 변수를 쉽게 찾아낼 수 O
||
는 첫 번째 truthy 값 반환??
는 첫 번째 정의된 (defined) 값 반환→ null
, undefined
, 0
구분 지을 때 중요
<script>
let height = 0;
alert(height || 100); //100 (height에 할당된 0을 falsy로 취급)
alert(height ?? 100); //0 (height에 0이 할당되어 defined로 생각)
</script>
??는 우선순위가 낮은 편이므로 사용 시 괄호 추가