Falsy values in JavaScript

The following are the 8 falsy values in JavaScript:

  1. false
  2. 0 (the same as 0x0, 0.0)
  3. -0 (the same as -0x0, -0.0)
  4. 0n (the same as -0n)
  5. empty string value (``, "", '')
  6. null
  7. undefined
  8. NaN

A more expanded list:

// I need to put the prettier-ignore to avoid converting the string quotes with prettier.
// prettier-ignore

// string
Boolean(''); // false
Boolean(``); // false
Boolean(''); // false

// 0
Boolean(0); // false
Boolean(0.0); // false, decimal
Boolean(0x0); // false, hexadecimal
Boolean(0o0); // false, octal
Boolean(0b0); // false, binary

// -0
Boolean(-0); // false
Boolean(-0.0); // false
Boolean(-0x0); // false

// 0n
Boolean(0n); // false
Boolean(-0n); // false
Boolean(0x0n); // false, hexadecimal
Boolean(0o0n); // false, octal
Boolean(0b0n); // false, binary

// weirdos
Boolean(NaN); // false
Boolean(null); // false
Boolean(undefined); // false

Boolean(false); // false

Why 8 values?

There are only 8 values because if the values are the same, you can't count them as another. And although equality is tricky in JavaScript there are real functional differences.

Let's go through each case by checking ==, === and Object.is:

Empty string value is all the same no matter the type of quote:

// prettier-ignore
"" == '' // true
'' == ``; // true

'' === ''; // true
'' === ``; // true

Object.is('', ''); // true
Object.is('', ``); // true

All the strings are the same, so they are only considered one value.

0 and +0 and 0.0 and 0x0 are the same

0 == +0; // true
0 === +0; // true
Object.is(0, +0); // true

0 == 0.0; // true
0 === 0.0; // true
Object.is(0, 0.0); // true

0 == 0x0; // true
0 === 0x0; // true
Object.is(0, 0x0); // true

0 and -0 are not the same!

This is the strange one.

0 == -0; // true
0 === -0; // true
Object.is(0, -0); // false! What?!

The Object.is returns false because 0 and -0 are not functionally identical:

1 / 0; // Infinity
1 / -0; // -Infinity

Why is this? Not sure, if you know let me know!

My assumption is that it fits with the formula 1/x. Go to google and search for plot 1/x. Pretty neat plot right?

On the chart plotted for 1/x you can see that when the y is going to Infinity, the x is approaching 0 from the positive side (+0).
On the chart plotted for 1/x you can see that when the y is going to -Infinity, the x is approaching 0 from the negative side (-0).

0n and -0n are the same Now you might be thinking that if 0 and -0 are not the same value, it should happen the same for 0n and -0n. But that is not true:

0n == -0n; // true
0n === -0n; // true
Object.is(0n, -0n); // true!

Why is it true? Because it's functionally identical. Even for the strange case:

1n / 0n; // Uncaught RangeError: Division by zero
1n / -0n; // Uncaught RangeError: Division by zero

My assumption here is, because BigInt's were introduced later they corrected the behavior. After all, every calculator complains when you try to divide 1 by 0, why not JavaScript too?

I'll compare null, undefined and NaN in another article.

Before there were "8 falsy values" on the MDN page, but I sort of inadvertently got rid of the count 😁

Mnemonic?

Lord of the Rings Mnemonic 1 empty string for the falsy under the sky 3 0s for the falsy doomed to die 3 weirdos for the falsy where the shadows lie And 1 false to rule them all and in the darkness bind them

This is one bad mnemonic.

Questions

What's the output?

Boolean(false);

What's the output?

Boolean(-0n);

What's the output?

// prettier-ignore
Boolean(``);

What's the output?

Boolean(0.0);

What's the output?

Boolean(-0.0);

What's the output?

Boolean(0x0);

What's the output?

Boolean(-0x0);

What's the output?

Boolean(-0x0n);

What's the output?

Boolean(0x0n);

What's the output?

Boolean(0o0);

What's the output?

Boolean(0b0);

What's the output?

// prettier-ignore
Boolean('');

What's the result of the division?

1 / 0;

What's the result of the division?

1 / -0;

What's the output?

Object.is(0, -0);

What's the output?

Object.is(0n, -0n);

What's the output?

// prettier-ignore
Boolean("");

What's the output?

Boolean(0);

What's the output?

Boolean(-0);

What's the output?

Boolean(NaN);

What's the output?

Boolean(null);

Boolean(undefined);

What's the output?

Boolean(0n);