Skip to content

Latest commit

 

History

History
40 lines (28 loc) · 837 Bytes

File metadata and controls

40 lines (28 loc) · 837 Bytes

08 Falsy and Truthy

In JavaScript, values are automatically converted to boolean when used inside conditions like if, while, or logical expressions.

This conversion is known as boolean coercion.

08.1 Falsy values

Falsy values are values that are converted to false when evaluated in a boolean context.

JavaScript has seven falsy values:

  • false
  • 0
  • 0n
  • ""
  • null
  • undefined
  • NaN
// Since 0 is a falsy value, the condition evaluates to false.
if (0) {
  console.log("This will not run");
}

08.2 Truthy values

Truthy values are values that are converted to true when evaluated in a boolean context.

Any value that is not falsy is considered truthy.

if ("JavaScript") {
  console.log("This will run");
}

Note: Empty arrays [] and empty objects {} are truthy.