본문 바로가기

Programming/JavaScript

Truthy and Falsy Values

5 falsy values: 0, '', undefined, null, NaN

console.log(Boolean(0));
// prints false
console.log(Boolean(undefined));
// prints false
console.log(Boolean('Jonas'));
// prints true
console.log(Boolean({}));
// prints true
console.log(Boolean(''));
// prints false

 

0은 Falsy Value이기 때문에-

else 문이 실행됨

const money = 0;
if (money) {
    console.log("Don't spend it all");
} else {
    console.log("나가서 돈을 벌어오쟈");
}
// prints 나가서 돈을 벌어오쟈

 

undefined는 falsy value이기 때문에 else문이 실행됨

let height;
if (height) {
    console.log('YAY! Height is defined');
} else {
    console.log('Height is UNDEFINED')
}
// prints Height is UNDEFINED