자바스크립트 연산자  || (OR) , ?? (Nullish coalescing operator, 널 병합 연산자) 다른점



<script>

        let obj = {

            v1: false,

            v2: true,

            v3: 0,

            v4: 1,

            v5: "",

            v6: null,

            v7: undefined,

        };

 

        for (let n of Object.getOwnPropertyNames(obj)) {

            let v = obj[n];

            console.log(nvv || "||"v ?? "??");

        }

</script>

 

//------------------

* 결과

v1 false || false

v2 true true true

v3 0 || 0

v4 1 1 1

v5  || 

v6 null || ??

v7 undefined || ??



//--------------------------

* false로 판정되는 경우

||  : false, 0, '', null, undefined

?? : null, undefined



 

//----------------------------

//참고

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_operator

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators




 

반응형
Posted by cd cd1

댓글을 달아 주세요