Truthy & Falsy

Values that evaluate to true or false.

Examples

Checking Falsy

0 is a falsy value.

let x = 0;
if (x) {
  console.log("Truthy");
} else {
  console.log("Falsy");
}
// Output: Falsy

Checking Truthy

Non-empty strings are truthy.

let name = "John";
if (name) {
  console.log("Hello " + name);
} else {
  console.log("Name is missing");
}
// Output: Hello John

Empty Array/Object

Empty arrays and objects are truthy.

console.log(Boolean([])); // true
console.log(Boolean({})); // true

Test Your Knowledge

JavaScript Quiz

No quiz available for this topic yet.