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:
- undefined : typeof instance === "undefined"
- Boolean : typeof instance === "boolean"
- Number : typeof instance === "number"
- String : typeof instance === "string"
- BigInt : typeof instance === "bigint"
- Symbol : typeof instance === "symbol"
Structural Types:
- 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;
- 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:
- 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?