2.1.10 널 병합/옵셔널 체이닝

    ES2020에서 추가된 ??(널 병합(nullish coalescing)) 연산자와 ?.(옵셔널 체이닝(optional chaining)) 연산자입니다.

    널 병합 연산자는 주로 || 연산자 대용으로 사용되며, falsy(0, '', false, NaN, null, undefined)nullundefined만 따로 구분합니다.

    const a = 0;
    const b = a || 3; // || 연산자는 falsy 값이면 뒤로 넘어감
    console.log(b); // 3
    
    const c = 0;
    const d = c ?? 3; // ?? 연산자는 null과 undefined일 때만 뒤로 넘어감
    console.log(d); // 0;
    
    const e = null;
    const f = e ?? 3;
    console.log(f); // 3;
    
    const g = undefined;
    const h = g ?? 3;
    console.log(h); // 3;
    신간 소식 구독하기
    뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.