1

I'm curious how many types there are in JS, because I have read conflicting answers from otherwise reliable sources:

If we look at the ECMAScript spec, there are 8 types:

  • Undefined, Null, Boolean, String, Symbol, Number, BigInt, and Object

It seems function is counted as object.


If we look at MDN, they say there is 9 types:

Six Data Types that are primitives, checked by typeof operator:

  1. undefined : typeof instance === "undefined"
  2. Boolean : typeof instance === "boolean"
  3. Number : typeof instance === "number"
  4. String : typeof instance === "string"
  5. BigInt : typeof instance === "bigint"
  6. Symbol : typeof instance === "symbol"

Structural Types:

  1. Object : typeof instance === "object". Special non-data but Structural type for any constructed object instance also used as data structures: new Object, new Array, new Map, new Set, new WeakMap, new WeakSet, new Date and almost everything made with new keyword;
  2. Function : a non-data structure, though it also answers for typeof operator: typeof instance === "function". This is merely a special shorthand for Functions, though every Function constructor is derived from Object constructor.

Structural Root Primitive:

  1. null : typeof instance === "object". Special primitive type having additional usage for its value: if object is not inherited, then null is shown;

Which one of these is correct?

TylerH
  • 20,799
  • 66
  • 75
  • 101
ZiiMakc
  • 31,187
  • 24
  • 65
  • 105
  • I think you're looking for opinions here, which isn't really what stack overflow is for. That said, I'd lean towards the spec over mdn. –  Apr 29 '21 at 11:06
  • 1
    Update your spec link: https://262.ecma-international.org/11.0/#sec-ecmascript-data-types-and-values . There are 8 of them. – georg Apr 29 '21 at 11:07
  • Your last 3 Structural Types in MDN are all of type "object", so it's total goes back to 7, that is how much different values `typeof` can return (as `function` is also "object") – Justinas Apr 29 '21 at 11:07
  • @georg thanks, but what about function? Is it a type? – ZiiMakc Apr 29 '21 at 11:09
  • 2
    @ZiiMakc: nope, function is an object. – georg Apr 29 '21 at 11:09
  • @georg `typeof (function () {})` yields `"function"` though.. –  Apr 29 '21 at 11:11
  • @georg so we should stick to spec and answer 8? – ZiiMakc Apr 29 '21 at 11:11
  • 1
    @ZiiMakc: yes, that's correct. – georg Apr 29 '21 at 11:11
  • Are you ready for this? String is an object. Integer is also an object in JavaScript. When you set `var i = 10`, you can right after convert it - for example - to string, using its `toString()` function.` – Gil Apr 29 '21 at 11:13
  • 2
    @CallumMorrisson: what `typeof` returns is not necessarily the actual type, see the table here https://262.ecma-international.org/11.0/#sec-typeof-operator – georg Apr 29 '21 at 11:13
  • 3
    @Gil: that's not true. strings are not objects and neither are numbers. – georg Apr 29 '21 at 11:17
  • @Gil Everything in JS is derived from `Object` but that doesn’t mean everything is an Object. `(() => {}) instanceof Object` is true but `1 instanceof Object` is false. So, we can say function is of object type while *number* and *staring* are not. While *Number* and *String* are object as `Number instanceof Object` is true. – Ashish Apr 29 '21 at 11:25
  • @georg Not by itself, no. :) Note that I wrote "an object in _JavaScript_". When you set an int, JavaScript uses [a primitive wrapper object](https://developer.mozilla.org/en-US/docs/Glossary/Primitive#primitive_wrapper_objects_in_javascript) to handle it. When you work with it you actually work with the wrapper. – Gil Apr 29 '21 at 11:25
  • `null` isn’t structural. `typeof null` is considered an unfixable bug. _“if object is not inherited, then null is shown”_ — that’s not the only use for `null` and this property is somewhat arbitrary. See [this 2ality article](https://2ality.com/2021/01/undefined-null-revisited.html) about the different usages of `null` vs. `undefined`. _Null_ itself is still its own type, though. – Sebastian Simon Apr 29 '21 at 13:53
  • @Gil When accessing properties from primitives, they are _coerced to objects_ (an operation which can be optimized by the engine). But the values themselves are still primitives: they are immutable and self-identifying (i.e. the value is identified by the value itself as opposed to, say, a reference to the value) values; `"" instanceof Object` is `false`, as already mentioned; and `Object(str) !== str` for any string `str`; same for numbers. [This answer](/a/9109037/4642212), where you probably got this idea from, is wrong. – Sebastian Simon Apr 29 '21 at 13:59

1 Answers1

5

The current edition of the ECMAScript spec defines 8 value types:

  1. Undefined
  2. Null
  3. Boolean
  4. String
  5. Symbol
  6. Number
  7. BigInt
  8. Object

https://262.ecma-international.org/11.0/#sec-ecmascript-language-types

The typeof operator is a big source of confusion in JavaScript, because what it returns is not always the actual type of the value. The conversion table for typeof (https://262.ecma-international.org/11.0/#sec-typeof-operator) is like this

Type of val Result
Undefined undefined
Null object !!!
Boolean boolean
Number number
String string
Symbol symbol
BigInt bigint
Object (does not implement [[Call]]) object
Object (implements [[Call]]) function !!!

Note the two exceptions marked with !!!

To confuse us further, the language also provides wrapper functions for these 4 primitive types

  1. Boolean
  2. Number
  3. String
  4. BigInt

These functions

  • when called with new, return their argument converted to a corresponding wrapper object (Boolean, Number etc)

  • when called without new, return their argument converted to a corresponding primitive value (Boolean, Number etc)

These functions are also called implicitly (in the new or "constructor" mode) when a primitive is used in the "object" context (e.g. "foo".length)

georg
  • 211,518
  • 52
  • 313
  • 390