?./??

김용진·2022년 6월 13일
0

|| 하고 ?? 차이

  • ||는 0, "", false, undefined 같은 falsy 값을 전부 검사 하는 연산자
  • ??는 undefined 하고 null 같은 nullish 만 검사하는 연산자입니다.
const a = false || "어떻게";
// a => "어떻게"
const b = false ?? "사람 이름이";
// b => false
const c = undefined ?? null ?? "엄",
// c => "엄"
  • ?.은 객체값이 비어있으면 에러말고 undefined라도 뜸
var user ={
  name : "kim",
  //age : {value : 34}
}

console.log(user.age.value) //value없다고 에러
console.log(user.age?.value) //undefined

(.이 하나일때는 ?없어도 알아서 undefined해주긴 함)
(?왼쪽 값이 늦게 도착할 때 활용 가능 user.age ?? "loding")

profile
개발 블로그

0개의 댓글