
A && B
A가 true면 B를 실행
- &&
const condi = true;
let i = 0;
const res = condi && i++; // i = 1
const condi2 = false;
const res2 = condi2 && i++ // i = 1
A || B
A가 false면 B를 실행
- ||
const condi = false;
let i = 0;
const res = condi || i++; // i = 1
const res2 = true || i++; // i = 1
A ?? B
A가 null과 undefined면 B를 실행
- ??
const req = 0;
let i = 0;
const res = req ?? i++ // i = 0
const res2 = null ?? i++ // i = 1
A?.B
A와 B의 값이 없다면 undefined return
- ?.
const array = [1, 2, 3, 4, 5];
if (array[-1][-1]) {
console.log("실행") // array[-1][-1]이 존재하지 않으니 오류
}
if (array[-1]?.[-1] === undefined) {
console.log("실행") // 실행
}
'언어 > 자바스크립트' 카테고리의 다른 글
JavaScript join split (0) | 2022.02.17 |
---|---|
JavaScript indexOf (0) | 2022.02.17 |
JavaScript 구조 분해 할당 (0) | 2022.02.10 |
JavaScript filter (0) | 2022.02.07 |
JavaScript every some (0) | 2022.02.07 |