Falsy는 null, undefined, 0, '', NaN 이 다섯가지를 뜻한다.
Truthy는 Falsy가 아닌 것이니 !Falsy다.
console.log(!null); // true
console.log(!undefined); // true
console.log(!0); // true
console.log(!''); // true
console.log(!NaN); // true
아래와 같이 truthy, falsy에 대한 검증을 할 수 있다.
let value = "트루티한 abc";
console.log(value || "출력값은 falsy 입니다."); // "트루티한 abc"
value = null;
console.log(value || "출력값은 falsy 입니다."); // "출력값은 falsy 입니다."
JSON 키 값이 없거나 falsy할 때 아래와 같이 적용할 수 있다.
const person = { };
if(!person.name) person.name = 'unknown';
'IT공간 > JavaScript' 카테고리의 다른 글
Spread Operator (전개 연산자) (0) | 2022.05.19 |
---|---|
비구조화 할당 (0) | 2022.05.16 |
[jQuery] jsonp 특징, 그리고 버그 (0) | 2021.04.21 |
JSON key 쉽게 사용해보자 (0) | 2021.04.14 |
크롬 디버깅 2 : 쿠팡 장바구니 바꿔치기 (0) | 2021.04.11 |