
[JS] 병합 연산자 (nullish coalescing operator) 사용 방법
Frontend/Javascript
2023. 5. 13. 16:33
병합 연산자란 null이나 undefined를 체크하고자 할때 사용한다. 현업에서 null이나 undefined 체크를 할때 나의 경우는 or연산자를 많이 써서 체크했었다. 하지만 숫자 0의 경우는 false로 구분하기에 자칫 실수를 할 수도 있다. ES11 문법에서 추가된 내용으로 병합 연산자를 소개하고자 한다. 첫째로 아래와 같이 변수들을 정의해 보았다. const isZero = 0; const isNull = null; const isUndefined = undefined; const isEmpty = ''; 1. OR 연산자를 쓸 경우 //# OR 연산자 const orCode1 = isZero || 'this is [Null] or [Undefined]'; const orCode2 = isNu..