On an MDN page about string methods (https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/Useful_string_methods), it says "We've said it before, and we'll say it again — everything is an object in JavaScript." But another MDN page states there are 7 data types in JavaScript, 6 primitives and object (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types). This is simply a mistake, correct? Or is there something I'm missing?
-
6MDN is a Wiki, so anybody can edit it. – Pointy Apr 30 '19 at 18:31
-
2Maybe [How is almost everything in Javascript an object?](https://stackoverflow.com/questions/9108925/how-is-almost-everything-in-javascript-an-object) could help you – Maheer Ali Apr 30 '19 at 18:33
-
4That article is just incorrect. I've logged a request for update (I could do it but updating those big "blog post" pages is a real pain and I'm lazy). – Pointy Apr 30 '19 at 18:35
-
1The article is an oversimplification that might be okay for a tutorial. The title of the article is "Strings *as* objects*. A string isn't an object, but can be treated as if it is an object. Whether or not the distinction should be made in a tutorial is a judgment call that can't be answered independently of the target audience. – John Coleman Apr 30 '19 at 18:40
-
I agree with @JohnColeman. It's easy to get lost in the weeds quickly if you need to be very precise. Otherwise you have a lot of trouble explaining why the function `"hello".hasOwnProperty()` is defined and is inheriting from `Object`. The explanation that everything is more or less an object, is the most useful until you really need the nuances (which in my experience is useful for avoiding downvotes here, but not much else). – Mark Apr 30 '19 at 18:47
-
A string can be treated as if it were an object right up to the point you try to give it a property. – Pointy Apr 30 '19 at 20:41
3 Answers
My terminology may be off, and I'm not one to pay attention to details like these, but maybe this helps clarify it a bit.
For example, number variables have the toExponential
method.
E.g., this is invalid 3.toExponential(2)
, but this is totally valid let a = 3; a.toExponential(2);
. On the other hand, both typeof 3
and typeof a
returns the string 'number'
.
So there's a distinction between what's an 'object' (roughly, what can have methods) and the data type a variable or literal can reference.

- 11,151
- 2
- 30
- 46
-
2That's a bad example — it's a syntax issue that happens because JS doesn't know what a `.` means in that context. `(3).toExponential(2)` is fine. – Mark Apr 30 '19 at 18:36
-
1What's happening there is that the runtime implicitly makes a Number instance (an object) from the number value (a primitive, not an object). – Pointy Apr 30 '19 at 18:37
-
2Note `3..toExponential(2)` also works (for syntax reasons). Also, to expand on @Pointy's comment about objects vs primitives, consider `var n = 3; n.foo = "bar"; console.log(n.foo);` prints `undefined`, but `var n = new Number(3);` would work fine. This is because `3` is a number primitive, but `new Number(3)` is a Number object. – p.s.w.g Apr 30 '19 at 19:25
I think it is a poor choice of phrasing. Things that are passed by reference are objects (array, or plain object, functions etc). The primitive data types are passed by value and I would consider null to be a keyword (it is also an object). The correct phrase would be:
"Everything can be represented as an object"
This would clear the misconception, as others pointed out, the only way you can call functions from Number.prototype or String.prototype is for the engine to implicity wrap the primitive as an object. The same way, you can do this (function(){console.log(this)}).call(5) //Number {5}

- 5,643
- 3
- 16
- 22
-
Null is one of the primitives (although typeof incorrectly states it's an object) – nCardot Apr 30 '19 at 19:05
-
Technically. The safest thing to ever say is that it is 8 byte pointer on address 0x00 that points to nothing. Many people will say it is a bug based on the 1-3 bit tags created by the engine. It cannot be changed, and today as the last "object" in the prototype chain, I'd say it makes much more sense for it to be considered as object. – ibrahim tanyalcin Apr 30 '19 at 20:04
every data type is instance of it's own class. like for example string. any string is instance of Class String all instance inherits methods from String class. methods are available only to object so you can consider that string is object to. but for sake continence all datatypes exists..
i tried to explain the concept but it's complicated

- 367
- 1
- 7
-
1
-
1Compare `"hello" instanceof String` and `new String("hello") instanceof String` – Mark Apr 30 '19 at 18:38