DEV Community

Cover image for NUll vs Undefined

Posted on


NUll vs Undefined

In JavaScript we have seven primitives:

  • string
  • number
  • boolean
  • null
  • undefined
  • symbol
  • bigInit

We often think that null and undefined are the same.
Let's try to use strictly equal operator with them:
Alt Text
they are not equal at all.
Undefined means that we have not yet attributed a value;
while null is a value that indicates nothingness
Alt Text

Top comments (2)

nicom64 profile image
nicom64 • Edited

You know you can find the type of a value with the typeof command :

typeof 'string'      // --> "string"
typeof 10            // --> "number"
typeof true          // --> "boolean"
typeof undefined     // --> "undefined"

Just remember : the type of the value null should be normally null, but it's not !

typeof null          // --> "object"

It's a very interesting story about a little bug insidiously introduced at the very beginning of the JS implementation (a tiny neglected of an if statement fot this type !) described here, and will never be fixed (too late !).

However, this tale highly helps me remember the case !

fennecdjay profile image
Jérémie Astor

In Gwion, I actually implemented @null type (the type of null) as inheriting from Object, as it means absence of an object. It's true this way needs a few special rules to match, say, function pointers but it works well this way.

Need a better mental model for async/await?

Check out this classic DEV post on the subject.

⭐️🎀 JavaScript Visualized: Promises & Async/Await

async await