JS-036
Visual JavaScript guide to Truthy: Understand which values JavaScript treats as true in a Boolean context
Beginner4 min~4 min read#truthy#boolean#logic

Truthy

Behaves like true

Goal

Understand which values JavaScript treats as true in a Boolean context.

Explanation

  • Truthy values are values that become true when converted to Boolean.
  • Most values in JavaScript are truthy.
  • Examples include 42, "Hello", [], {}, and -5.
  • In if statements, JavaScript automatically checks whether a value is truthy or falsy.
  • Understanding truthy values is very important when working with conditions.

Analogy

Imagine a checkpoint. Most values pass the check and receive true status.

Code

console.log(Boolean(42));
console.log(Boolean("Hello"));
console.log(Boolean([]));

Result: true true true

Common mistake

Beginners often think that any unusual value becomes false. In fact, most values convert to true.

Remember

  • Truthy values become true.
  • Most JavaScript values are truthy.
  • Non-empty strings are truthy.
  • Arrays and objects are truthy.

Quick Quiz

Which value is truthy?

Resources