[javascript] Truthy, Falsy

jinwonShen·2025년 1월 25일

javascript

목록 보기
20/52
post-thumbnail

Truthy(참으로 평가되는 값)

간단한 예시

if (true) {
  console.log("참!");
}
if ({}) {
  console.log("참!");
}
if ([]) {
  console.log("참!");
}
if (42) {
  console.log("참!");
}
if ("0") {
  console.log("참!");
}
if ("false") {
  console.log("참!");
}
if (new Date()) {
  console.log("참!");
}
if (-42) {
  console.log("참!");
}
if (12n) {
  console.log("참!");
}
if (3.14) {
  console.log("참!");
}
if (-3.14) {
  console.log("참!");
}
if (Infinity) {
  console.log("참!");
}
if (-Infinity) {
  console.log("참!");
}
// ...

Falsy(거짓으로 평가되는 값)

간단한 예시

if (false) {
  console.log("거짓..");
}
if (null) {
  console.log("거짓..");
}
if (undefined) {
  console.log("거짓..");
}
if (0) {
  console.log("거짓..");
}
if (-0) {
  console.log("거짓..");
}
if (NaN) {
  console.log("거짓..");
}
if (0n) {
  console.log("거짓..");
}
if ("") {
  console.log("거짓..");
}
// 공백도 문자로 표기하기에 공백이 있으면 '참' 없다면 '거짓'으로 판단

따라하기


profile
하면 된다. | 좋은 FE 개발자 되기

0개의 댓글