Falsy는 크게 5가지가 있다. 이외에는 모두 true다.
let test = "";
console.log(!test) // true
let test = 0;
console.log(!test) // true
let test = undefined;
console.log(!test) // true
let test = null;
console.log(!test) // true
let test = "hi"/0;
console.log(!test) // true
하.지.만. 오늘 궁금한 건 이미 알고 있는 falsy에 대한 것이 아니었다. 만약 배열이 비어있으면 true일까 false일까? 빈 객체는? 빈 배열에 빈 문자열만 있으면?
궁금해서 해봤다.
let array = []; let obj = {}; console.log(!array) // false console.log(!obj) // false
let array = [undefined]; console.log(!array) // false
let array = ["hi"/2]; console.log(!array) // false
let array = [null]; console.log(!array) // false