`truthy` and `falsy` in JavaScript

`truthy` and `falsy` in JavaScript

This article explains truthy and falsy in JavaScript.

YouTube Video

truthy and falsy in JavaScript

The concepts of truthy and falsy in JavaScript play a very important role, especially in the evaluation of conditional statements. Truthy and falsy determine whether a value is evaluated as true (true) or false (false), but these values are not necessarily the boolean true or false themselves.

Falsy Values

Falsy values refer to values that are evaluated as false in JavaScript conditionals. Below is a list of falsy values in JavaScript.

 1const values = [
 2    false,
 3    0,
 4    -0,
 5    0.0,
 6    -0.0,
 7    0n,
 8    -0n,
 9    "",
10    null,
11    undefined,
12    NaN
13];
14
15for (let value of values) {
16    if (value) {
17        console.log(`${value} is truthy`);
18    } else {
19        console.log(`${value} is falsy`);
20    }
21}

In this example, all falsy values will be output as "falsy". Below is a list of falsy values in JavaScript.

  • falsefalse in Boolean type
  • 0 — The number zero (decimal 0.0 is also falsy)
  • -0 — Negative zero (technically distinct from 0, but still falsy)
  • 0n — Zero in BigInt type
  • "" — An empty string of length 0
  • null — No value present
  • undefined — Undefined value
  • NaN — Not a Number

These values are treated as false in logical operations and conditional statements (such as if statements).

Truthy Values

All values that are not falsy are truthy. This refers to values that are evaluated as boolean true. In JavaScript, many values are treated as truthy.

 1const values = [
 2    true,
 3    1,
 4    -1,
 5    "Hello",
 6    " ",
 7    [],
 8    {},
 9    function() {},
10    Symbol(),
11    Infinity
12];
13
14for (let value of values) {
15    if (value) {
16        console.log(value, ' is truthy');
17    } else {
18        console.log(value, ' is falsy');
19    }
20}

In this code, truthy values are output as "truthy". For example, the following values are truthy.

  • truetrue in Boolean type
  • Numbers (other than 0) — For example, 1 and -1 are also truthy.
  • Strings (non-empty) — for example, "Hello" or " " (even strings with only spaces are truthy)
  • Objects — Even empty objects ({}) and arrays ([]) are truthy.
  • Functions — Functions themselves are also truthy.
  • Symbol — Values of type Symbol are also truthy.
  • Infinity — Both positive and negative infinity are truthy.

Applications of truthy and falsy

The concepts of truthy and falsy are often used in conditionals and logical operations. For example, when setting a default value using the || (logical OR) operator, the first truthy value is returned as the result.

How to check for falsy values

To check for falsy values, using the Boolean function is convenient. The Boolean function explicitly converts a given value to a boolean type.

Examples of the Boolean function

 1const values = [
 2    false,
 3    0,
 4    "",
 5    null,
 6    undefined,
 7    NaN
 8];
 9
10for (let value of values) {
11    console.log(`${value} is ${Boolean(value) ? 'truthy' : 'falsy'}`);
12}

Conclusion

Truthy and falsy in JavaScript are important for understanding how non-boolean values are evaluated in conditional expressions. Falsy values include false, 0, an empty string, null, undefined, and NaN, while all other values are evaluated as truthy. Understanding these concepts enables you to write more flexible and efficient code.

You can follow along with the above article using Visual Studio Code on our YouTube channel. Please also check out the YouTube channel.

YouTube Video