Skip to content

Hello World!

Published: at 22:36

A very big hello world from me to you!

Ahmed here with yet another blog site, but I think I like this one! I might keep it around!

While I have you here, let’s play a game since I love JavaScript.

Javascript - I love it

What do you think is the type of “null”?

console.log(typeof null);

If you are a rational being, you would guess null is of type null or something falsy.

You’d be wrong though, JavaScript, one of the most popular languages of our day is known to have a bug as Brendan Eich in the typeof operator. See this 2006 proposal for ES4 which says:

From bug fixes where the typeof fixing started, more about the typeof null === “null” fix:

  • Spidering the web shows no dependencies on typeof null === “object”.
  • Spidering discloses a number of pages that will suffer a ReferenceError if null is passed.
    • I.e., these pages contain scripts that use typeof foo == “object” to guard foo dereferences.

Note that if we make RegExp have a [Call] internal method, typeof /hi/ === “function”. In IE, typeof alert === “object” even though alert is callable (but of course not a function object, so not applyable, etc.). Do we really want all callable objects to be of type “function”?

In general, typeof seems like a mess that will be hard to reform sensibly. If we provide a builtin::type() function, or perhaps reflect::type(), we could do something much better, and stop investing in typeof. Comments?

— Brendan Eich 2006/03/31 15:13

Alas, what happens to websites that already used that typeof null === 'object' business?

Douglas Crockford noted in the proposal:

I think it is too late to fix typeof. The change proposed for typeof null will break existing code. For example, isNull functions have been written by idiots as

function isNull(a) {
  return typeof a == "object" && !a;
}

It is possible to write an isNull that performs correctly with the fixed typeof, but existing code cannot anticipate the change.

Perhaps a better alternative is a new operator, maybe called typeString, which works the way typeof should work, including

typeString [] === 'array'

The solution was that JavaScript was going to become a versioned language, whereby the browser should check what version of JavaScript is being interpreted (don’t JIT me please), and consequently use the ES4 interpreter as opposed to the default ES3 interpreter.

It’s known among js sleuths that this version of JavaScript never happened. As Fireship notes:

Back in the early 2000’s there was a version of JavaScript called ES4. It was going to include a type system but everybody disagreed on it so much that they just threw that entire version of JavaScript in the garbage and skipped ahead to ES5.

And thus, typeof null is and always (probably) will be null. It’s not a huge problem because you could still determine whether something is null or not using Douglas Crockford’s approach from 2006.

Perhaps it’s a godsend that JavaScript doesn’t release significant breaking changes. Perhaps it’s incredibly annoying that JavaScript still has broken syntax from its earliest days. I’ll leave it to you to make that value judgement, but I think it’s a bit of both.

astro is awesome! 😊