ES6 스터디 정리 : ?? Operator

Llux lux·2022년 5월 20일
0

ES6 스터디

목록 보기
15/21

?? 오퍼레이터

null, undefined 에 대해서 판단해서 Default Value 를 설정할 수 있도록 한다.

기존에 ||(or) 를 사용하여 Default Value 를 설정할 경우 0 으로 설정한 변수는 false 가 되어 잘못된 결과가 나올 수 있다.

let name;
console.log("hello", name || "anonymous"); //hello anonymous

name = 0;
console.log("hello", name || "anonymous"); //hello anonymous
//0이 할당 되었으나 0을 false 로 인식 해 default value 로 출력된다.

console.log("hello", name ?? "anonymous"); //hello 0
//name 에 0이 할당되었으므로 name 을 출력한다.
profile
하하하

0개의 댓글