Page of Alena's post with the title Falsy values, Abstract Equality Algorithm

⭐️ 💖

Back to home page

Alena Nikolaeva

Germany

Frontend Developer

Falsy values, Abstract Equality Algorithm

Updated: 27.07.2020

10 min

NNBUS stands for Null, Number, Boolean, Undefined, String

Something that I had to remember by heart while I was baby developer in the coding bootcamp.

And it hadn't had much meaning at all until once already in a job I asked my senior developer to check why my alert doesnt't show up and he said "Yeah, because "0" is false". That sounded weird. I had to look it up!

That popped up in my first Google search:

"In JavaScript, there is a specialist list of following 7 values, which are called falsy values — they all evaluate to false in conditionals"

It didn't make sense to me, but one phrase that I've seen in the article got my attention and I kept reading about falsy values and type coercion.

"...You can also think of 0 not being equal to -0: when it is +0, a tiny bit of matter, it can cost you nothing, such as a grain of sand. But if it is -0, which is a tiny bit of antimatter, it would cost you a lot of money, as the current price of 1 gram of antimatter is $62.5 trillion dollars..."

Here is the deal:

In Javascript there are values that are falsy:

  • 0
  • -0
  • null (primitive value that represents the intentional absence of any object value. Also known as "‘no value’ or nonexistence of any value." It's an object that has no properties, is non-mutable, and only a single instance of the same exists in the system at all times)
  • NaN (wierd thing, or number value that is a IEEE 754 “Not-a-Number” value)
  • false (member of the Boolean type)
  • undefined It's a global variable that JS creates at run time. JavaScript assigns ‘undefined’ to any object that has been declared but not initialized or defined. (- We also have new additions in ES2020 0n == false // true -0n == false // true)

In order to understand a falsy value, the good way to start is describing a truthy value.

Truthy: something that evaluates to TRUE.

Falsy: something that evaluates to FALSE.

JavaScript uses type conversion to coerce any value to a Boolean in contexts that require it. So the operation ToBoolean that takes and argument of types:

  • 0
  • -0
  • null
  • NaN
  • false and
  • undefined will return false as the result (Easy to remember when comparing to Number 0, -0, or NaN, will return false).

Here is the brief from developer.mozilla.org:

  • double equals (==) will perform a type conversion when comparing two things, and will handle NaN, -0, and +0 specially to conform to IEEE 754 (so NaN != NaN, and -0 == +0); Loose equality compares two values for equality, after converting both values to a common type.
  • triple equals (===) will do the same comparison as double equals (including the special handling for NaN, -0, and +0) but without type conversion; if the types differ, false is returned.
  • Object.is does no type conversion and no special handling for NaN, -0, and +0 (giving it the same behavior as === except on those special numeric values).
Boolean(undefined); // false
Boolean(null); // false
Boolean(false); // false
Boolean(0); // false
Boolean(""); // false
Boolean(NaN); // false

So “0” is equal to false because “0” is of type string but when it tested for equality the automatic type conversion of JavaScript comes into effect and converts the “0” to its numeric value which is 0 and as we know 0 represents false value. So, “0” equals to false.

I find the

Consider the silly example:

if (0) {
    alert('What will happen');
}

Nothing.

If we check the specs

  1. If Result(3) is false, return (normal, empty, empty).
  2. Evaluate Statement.
  3. Return Result(5).

"0" and '0' - they're non-empty strings, which are truthy, but Javascript's == matches numbers with equivalent strings (e.g. 42 == "42"). Since 0 == false, if "0" == 0, "0" == false

My alert would never run because of 0 being a falsey value, that doesn’t satisfy the if statement’s condition.



Not knowing the type or the effect on comparison can cost the company money and keeping those concepts fresh in your head can really help when debugging JavaScript applications.