Cardona Designs | Setting Standards

CAT | Javascript

Jun/11

18

Falsey values

In javascript conditional expressions are decided by the result of truthy or falsy statements. Here I intend to list all of the falsey values.

A way to test if a value is truthy or falsey is to run it through an if else statement and see which path it takes.

  if (condition) {
    console.log("truthy");
  } else {
    console.log("falsey");
  }

Here are a list of all falsey values. Please let me know if I missed any.

  1. undefined
  2. false
  3. 0
  4. null
  5. NaN

Open your dev console and then click on any of the buttons below to run that value through the above if statement to see whether it’s truthy or falsey.

No tags

Jun/11

17

Javascript Quirks

I know that this has been documented many places before but I wanted to do it again to better help myself remember. Here are a few deliciously quirky things about javascript.

typeof(null)

There are 2 bottom values in javascript that don’t inherit from the object prototype. They are undefined and null. However when you check the typeof(null); it returns object.

console.log(typeof(null)); // logs object which is wrong

typeof(NaN)

NaN (not a number) is another bottom value in javascript. When you check it’s type in ultra counter-intuitive fashion you get “number.” Nice.

console.log(typeof(NaN)); // logs "number" which is epic

NaN == NaN && NaN !== NaN

When you compare NaN to itself with double equality operators you get false. When you confirm this by making sure NaN is not double equal to itself you get true.

Crockford recommends using the triple equality operator when comparing bottom values.

No tags

Theme Design by devolux.nh2.me