Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Why is null an object?

Why is null an object?

Why is null an object in JavaScript?

Michal Skuza

April 12, 2017
Tweet

More Decks by Michal Skuza

Other Decks in Programming

Transcript

  1. JAVASCRIPT TYPE SYSTEM - QUIRKS ➤ 1 + 1 ➤

    1 + '1' ➤ {} + [] ➤ [] + {} ➤ {} - [] ➤ [] - {} ➤ ({} + []) === 0
  2. JAVASCRIPT TYPE SYSTEM - QUIRKS ➤ 1 + 1 //

    2 ➤ 1 + '1' // '11' ➤ {} + [] //0 ➤ [] + {} // '[object Object]' ➤ {} - [] // -0 ➤ [] - {} // NaN ➤ ({} + []) === 0 // false ➤ // sigh
  3. NULL - TYPE SYSTEM ➤ null is not undefined ➤

    null == undefined // true ➤ null === undefined // false // oh ➤ typeof null // 'object' // gosh, why?
  4. TYPE SYSTEM - ORIGINAL IMPLEMENTATION ➤ type tag 1-3 bit,

    values stored in 32 bit units ➤ 5 type tags 1. 000: object. The data is a reference to an object. 2. 1: int. The data is a 31 bit signed integer. 3. 010: double. The data is a reference to a double floating point number. 4. 100: string. The data is a reference to a string. 5. 110: boolean. The data is a boolean.
  5. WHY IS IT NOT FIXED YET? ➤ typeof null ===

    “null”; // true ➤ there was a proposal ➤ but it was rejected ➤ „I think it is too late to fix typeof. The change proposed for typeof null will break existing code.” Douglas Crockford
  6. CONCLUSIONS ➤ It can be tricky for people learning JavaScript

    ➤ Some decision can have a serious impact