
true || anything // true
false || anything // anything
true && anything // anything
false && anything // false

if(true) '완료' => true && '완료
if(!false) '미완료' => false || '미완료객체 타입을 원했던 elem이 null이어서 객체 프로퍼티를 참조하면 타입에러가 나는것을 방지
const elem = null;
const value1 = elem.value; //타입에러
const value2 = elem && elem.value; //null
매개변수의 기본값을 설정하여 undefined 발생을 방지
const getStringLength = (str) => {
str = str || '';
return str.length;
}
getStringLength(); //0
getStringLength('hi'); //2
=> ES6의 매개변수의 기본값 설정
const getStringLength = (str = '') => {
return str.length;
}
getStringLength(); //0
getStringLength('hi'); //2
null 또는 undefined인 경우 undefined를 반환, 그렇지 않으면 우항의 프로퍼티 참조const elem = null;
const value = elem?.value; //undefined'' 처럼 falsy값이면 왼쪽프로퍼티인 ''를 반환하지만, 옵셔널 체이닝 연산자는 null 또는 undefined가 아닌 falsy값이어도 우항의 프로퍼티 참조const str = '';
const length = str?.length; //0null 또는 undefined인 경우 우항의 피연산자를 반환, 그렇지 않으면 좌항의 피연산자를 반환const foo = null ?? 'default string'; //'default string'''처럼 falsy값이면 우항의 값을 반환하였는데, null병합 연산자는 왼쪽 피연산자가 falsy값이라도 null 또는 undefined가 아니면 왼쪽의 피연산자를 그대로 반환const foo = '' ?? 'defalt string'; // ''