In various answers on this site I have read conflicting statements about whether everything in JavaScript is an object or not.
What is the truth?
The idea is that the answers are based on reliable sources, preferably in the ECMAScript 2016 (version 7) but it may well be the one in which a specification element has been introduced in this regard.
Here are a couple of examples of each case
everything is an object
- Response to Swapping two objects by changing the result in JavaScript
- Answer to How does the prototype of an object work? 1
not everything is an object
- Answer to Why does instanceof of a literal number return false?
- Response to Arguments in functions (Javascript - ES5)
Ambiguous
- Answer to Inheritance of properties and methods according to data types
- The answer mentions that primitives can use methods "of their data type".
General note:
Although this question has an answer from me, the idea is to have the best explanation on this for the benefit of the community.
Specific Notes:
1 : This question has been marked as a duplicate because it was believed that the answer to this question is in the referenced answer, however, it is precisely pointing out the controversy that exists considering the answers to other questions. On the other hand, this answer does not base its arguments on external sources.
Short answer
As far as data types are concerned, in JavaScript there are two basic classes, primitives and objects, so not everything is an object in JavaScript , but beware, this might turn primitives into an object temporarily.
Demonstration
Explanation
With code
instanseof
is an operator to test whether the first operand, an object, is an instance of the second, a constructor. It follows the whole chain of prototypes, if it finds it it returnstrue
, if it doesn't find it it returnsfalse
.Based on the documentation
The following is a statement from the Mozilla Developer Network glossary, which very briefly clarifies that not everything in JavaScript is an object , my emphasis.
Primitive _
Of Grammar and Types
Below what I think are the sections of the ECMAScript 2016 (Version 7) that clarify this
Note that an object is not at the lowest level of the language implementation, unlike primitives.
Full explanation about data types and values in ECMAScript 2016 (version 7) is in §6 ECMAScript Data Types and Values
On the differences between primitives and primitive objects