02. Truthy and Falsy

Truthy and Falsy

์ด๊ฒƒ์€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฌธ๋ฒ•๊นŒ์ง€๋Š” ์•„๋‹ˆ์ง€๋งŒ, ์•Œ์•„๋‘ฌ์•ผ ํ•˜๋Š” ๊ฐœ๋…์ž…๋‹ˆ๋‹ค. Truthy: true ๊ฐ™์€๊ฑฐ... Falsy: false ๊ฐ™์€๊ฑฐ... ๋ผ๊ณ  ์ดํ•ด๋ฅผ ํ•˜๋ฉด ๋˜๋Š”๋ฐ์š”, ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ•จ์ˆ˜๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ด…์‹œ๋‹ค.

function print(person) {
  console.log(person.name);
}

const person = {
  name: 'John'
};

print(person);

๋งŒ์•ฝ์— ์ด๋Ÿฌํ•œ ์ƒํ™ฉ์—์„œ, ๋งŒ์•ฝ print ํ•จ์ˆ˜๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ๋น„์–ด์ง„ ์ฑ„๋กœ ์‹คํ–‰๋๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ด…์‹œ๋‹ค.

function print(person) {
  console.log(person.name);
}

const person = {
  name: 'John'
};

print();

์ด ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์—๋Ÿฌ๋ฅผ ์ƒ์„ฑํ•ด๋ƒ…๋‹ˆ๋‹ค.

TypeError: Cannot read property 'name' of undefined

์ด๋Ÿฌํ•œ ์ƒํ™ฉ์—์„œ, ๋งŒ์•ฝ์— print ํ•จ์ˆ˜์—์„œ ๋งŒ์•ฝ์— object ๊ฐ€ ์ฃผ์–ด์ง€์ง€ ์•Š์•˜๋‹ค๋ฉด, ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค๊ณ  ์ฝ˜์†”์— ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌํ˜„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

function print(person) {
  if (person === undefined) {
    return;
  }
  console.log(person.name);
}

const person = {
  name: 'John'
};

print();

๊ทธ๋Ÿฐ๋ฐ ๋งŒ์•ฝ์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด print ์— null ๊ฐ’์ด ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ „๋‹ฌ๋˜๋ฉด ์–ด๋–จ๊นŒ์š”?

function print(person) {
  if (person === undefined) {
    console.log('person์ด ์—†๋„ค์š”');
    return;
  }
  console.log(person.name);
}

const person = null;
print(person);

๊ทธ๋Ÿฌ๋ฉด ๋˜ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

TypeError: Cannot read property 'name' of null

๊ทธ๋Ÿฌ๋ฉด ๋˜.. print ํ•จ์ˆ˜์— ์กฐ๊ฑด์„ ์ถ”๊ฐ€ํ•ด์ค˜์•ผํ•ฉ๋‹ˆ๋‹ค.

function print(person) {
  if (person === undefined || person === null) {
    console.log('person์ด ์—†๋„ค์š”');
    return;
  }
  console.log(person.name);
}

const person = null;
print(person);

์ด๋ ‡๊ฒŒ person ์ด undefined ์ด๊ฑฐ๋‚˜, null ์ธ ์ƒํ™ฉ์„ ๋Œ€๋น„ํ•˜๋ ค๋ฉด ์œ„์™€ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์•ผํ•˜๋Š”๋ฐ์š”, ์—ฌ๊ธฐ์„œ ์œ„ ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ถ•์•ฝํ•ด์„œ ์ž‘์„ฑ ํ•  ์ˆ˜ ์žˆ๋‹ต๋‹ˆ๋‹ค.

function print(person) {
  if (!person) { // ์ถ•์•ฝํ•ด์„œ ์ž‘์„ฑ ํ•œ ์ฝ”๋“œ
    console.log('person์ด ์—†๋„ค์š”');
    return;
  }
  console.log(person.name);
}

const person = null;
print(person);

์ด๊ฒŒ ์ž‘๋™ํ•˜๋Š” ์ด์œ ๋Š”, undefined ์™€ null ์€ ๊ธฐ๋ณธ์ ์œผ Falsy ํ•œ ๊ฐ’์ž…๋‹ˆ๋‹ค. Falsy ํ•œ ๊ฐ’ ์•ž์— ๋Š๋‚Œํ‘œ๋ฅผ ๋ถ™์—ฌ์ฃผ๋ฉด true ๋กœ์ „ํ™˜๋ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•ด๋ณด์„ธ์š”.

console.log(!undefined);  //true
console.log(!null);       //true

Falsy๊ฐ’ ์ข…๋ฅ˜๋“ค

Falsy ํ•œ ๊ฐ’ 5๊ฐœ ์žˆ์Šต๋‹ˆ๋‹ค.

console.log(!undefined);   //true
console.log(!null);        //true
console.log(!0);           //true
console.log(!'');          //true
console.log(!NaN);         //true

์ด ๊ฐ’์€ ๋ชจ๋‘ true ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

Falsy ํ•œ ๊ฐ’์€ ์œ„ ๋‚˜์—ดํ•œ ๋‹ค์„ฏ๊ฐ€์ง€ ์ž…๋‹ˆ๋‹ค.! ๊ทธ๋ฆฌ๊ณ , ๊ทธ ์™ธ์˜ ๋ชจ๋“  ๊ฐ’์€ Truthy ํ•œ ๊ฐ’์ž…๋‹ˆ๋‹ค.

Truthy๊ฐ’ ์ข…๋ฅ˜๋“ค

console.log(!3);                //false
console.log(!'hello');          //false 
console.log(!['array']);        //false 
console.log(![]);               //false
console.log(!{ });              //false 

์ด๋ฒˆ์—๋Š” ์•„๊นŒ์™€๋Š” ๋ฐ˜๋Œ€๋กœ ๋ชจ๋“  ๊ฐ’์ด false ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

์ถ”๊ฐ€์ ์œผ๋กœ, ์•Œ์•„๋‘๋ฉด ์œ ์šฉํ•œ ํŒ ํ•˜๋‚˜๋ฅผ ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ์—, ํŠน์ • ๊ฐ’์ด Truthy ํ•œ ๊ฐ’์ด๋ผ๋ฉด true, ๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด false ๋กœ ๊ฐ’์„ ํ‘œํ˜„ํ•˜๋Š” ๊ฒƒ์„ ๊ตฌํ˜„ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

const value = { a: 1 };

const truthy = value ? true : false;

์ด์ „์— ๋ฐฐ์šด ์‚ผํ•ญ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์‰ฝ๊ฒŒ value ๊ฐ’์˜ ์กด์žฌ ์—ฌ์— ๋”ฐ๋ผ ์‰ฝ๊ฒŒ true ๋ฐ false ๋กœ ์ „ํ™˜์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ, ์ด๋ฅผ ๋” ์‰ฝ๊ฒŒ ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

const value = { a: 1 };
const truthy = !!value;
console.log(truthy)        //true

!value ๋Š” false ๊ฐ€ ๋˜๊ณ , ์—ฌ๊ธฐ์— !false ๋Š” true ๊ฐ€ ๋˜์–ด์„œ, ๊ฒฐ๊ณผ๋Š” true ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

Last updated