[jsp]Boolean, (and, or), null

김지민·2023년 11월 30일
0

Boolean 타입

true, fasle

falsy값에는
false, null, undefined, 0, NaN, ''(빈 문자열)

나머지 true {} []도 true값

for (let i = 4; i; i = i - 2) {
  console.log(flowers[i]);
} // 0이 되면 boolean false 

조건문, 반복문등에 나오니 개념 확실히 알고 가야된다.

(AND, OR)

console.log(a && b); // and
and 방식에서 a가 true일 경우 b를 출력
and 방식에서 a가 false일 경우 a를 출력

console.log(a || b); //or
or 방식에서 a가 true일 경우 a를 출력
or 방식에서 a가 false일 경우 b를 출력

and 와 or 이 같이 나오면 and의 우선순위가 더 높다.
console.log(true || false && false); // true

null 병합 연산자 ??

ES2020 - null 병합 연산자 'Nullish coalescing operator'
-->물음표 두 개(??)를 사용해서 null 혹은 undefined 값을 가려내는 연산자

const example1 = null ?? 'I'; // I
const example2 = undefined ?? 'love'; // love
const example3 = 'Codeit' ?? 'JavaScript'; // Codeit

console.log(example1, example2, example3); // I love Codeit

왼쪽 값이 null 또는 undefined 일때 오른쪽 값 출력

null 병합 연산자(??)는 왼편의 값이 null이나 undefined인지 확인
null이나 undefined가 아닌 falsy 값을 활용할 때 결과가 서로 다르다.

profile
내용 정리 잘해보자

0개의 댓글