일종의 위장??보자마자 머릿속은??그렇게 되지 않기 위해서.

ES 2020 문장의 문법 중 한 마디??딱 봐도 머릿속이 틀린 것 같아??그래서 사용법을 배웠어요.

엔리스 Coalescing Opertor


구문


leftExpr ?? rightExpr

사용법


const y = null;
const z = 2;
const x = y ?? z;
console.log(x); // 2

const y = 6;
const z = null;
const x = y ?? z;
console.log(x); // 6

const y = undefined;
const z = null;
const x = y ?? z;
console.log(x); // null

const y = null;
const z = undefined;
const x = y ?? z;
console.log(x); // undefined
MDN에 따라
정보
leftExpr ?? rightExpr
왼쪽이 비어 있거나 비어 있으면 오른쪽의 값을 되돌려줍니다. 그렇지 않으면 왼쪽의 값을 되돌려줍니다.
이렇게 쓰여 있다.코드sandbox로 실험을 해보면??null이나 undefined가 아닌 것을 z로 되돌려 주는 것 같습니다.
(실제로는 y의 값을 평가해야 한다. 만약에 y가null 또는 y가undefined라면 z와 같은 글씨를 되돌려준다.😅)
우선순위는 y의 값을 평가하는 →y가null 또는 y가undefinded라면 z의 값을 평가하는 순서다.

??=연산자 정보


이것은 일종이다및 대입된 "="조합 연산자.

구문


expr1 ??= expr2

사용법


let y = 3;
const z = 5;
y ??= z;
console.log(y); // 3

let y = null;
const z = 5;
y ??= z;
console.log(y); // 5
??=는 다음과 같습니다.
x ?? (x = y);
즉, x가null이 아니거나 x가undefined가 아니라면 y의 값을 x에 대입한다.
자주 대입하는 아래의 부등가
x = x ?? y;

Optional Chaning(정보...)


사용법


const object = {
  property: {
    property: {
      property: "value"
    }
  }
}
 
if (object?.property?.property?.property) {
  object.property.property.property = "new value";
}
"?."비어 있거나 비어 있을 때의undefined를 되돌려줍니다.
내포된 객체의 값을 확인하는 데 도움이 됩니다.

기타


다른 사람의 재난?의 구문에 세 개의 연산자가 있습니다.
이 보도에서python에 대해 js를 비교하고 세 가지 연산자의 쓰기를 조사했다.

참고 자료


https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Operators/Logical_nullish_assignment
https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Operators/Optional_chaining
https://mcro.tech/blog/es2020/
https://zenn.dev/tonkotsuboy_com/articles/es2021-whats-new

좋은 웹페이지 즐겨찾기