Is everything in JavaScript an Object?
Tags: javascript, webdev, programming, react
Published: 2021-07-10 19:59:53

Well well! The motivation for this post comes from my [last post](https://dev.to/mayankav/are-all-javascript-functions-closures-27kp) where I had a comment from one of my readers that I couldn't agree with. I was talking about functions and closures [(also how closures work internally)](https://dev.to/mayankav/are-all-javascript-functions-closures-27kp) and how every function in JavaScript is indeed a closure on its surrouding scope but then somebody said, ***aren't all entities in JavaScript objects?*** I didn't see that coming but this wasn't the first time somebody put this up. So you will find me trying to break this down practically in this post.

If you can, bring up your browser's console and try creating a simple variable and give it a string value. How did you do it? Did you write something like 
```javascript 
var myString = "That's my string";
``` or did you choose to go with 
```javascript 
var myString = new String("That's my string");
``` For a fact, I know you didn't choose the last method. I mean no body does and if you want to prove me wrong and say that you did infact choose the second one, well somebody will then probably ask you, why? MDN says "[String primitives and string objects can be used interchangeably in most situations](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String?redirectlocale=en-US&redirectslug=JavaScript/Reference/Global_Objects/String#:~:text=String%20primitives%20and%20string%20objects%20can%20be%20used%20interchangeably%20in%20most%20situations.)". MDN then also says "[A Primitive is data that is not an Object](https://developer.mozilla.org/en-US/docs/Glossary/Primitive#:~:text=a%20primitive%20(primitive%20value%2C%20primitive%20data%20type)%20is%20data%20that%20is%20not%20an%20object)". The second statement makes the answer clear "***Not everything in JavaScript is an Object***" and this can easily be verified by using the "[typeof operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof)". We still need to clarify the pin size difference. With an example?

```javascript 
let myString = "That's my string";
let yourString = new String("That's your string");

console.log(typeof myString); // outputs "string"
console.log(typeof yourString); // outputs "object"

//MDN says primitives and objects can SOMETIMES be interchangeably used
let yourBigString = yourString.toUpperCase();
console.log(yourBigString); // outputs "THAT'S YOUR STRING"
let myBigString = myString.toUpperCase();
console.log(myBigString); // outputs "THAT'S MY STRING"
```
<center><h6>[Try this on codepen](https://codepen.io/mayankav/pen/vYmGYpo)</h6></center><br>

Does that ring a bell? We do often use primitives and objects interchangeably because JavaScript makes it possible for primitives to somehow use the methods designed into real Objects. Using a primitve value shall give you the benefit of writing concise without losing on the comfort of using easy methods to manipulate and process the values. When you call a String object's method on a primitive string value, to make this work your primitve is first wrapped into an appropriate Wrapper class (String in this case). The method you want to call is called on the transient object which is discarded as soon as the result is returned. [Read on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String?redirectlocale=en-US&redirectslug=JavaScript/Reference/Global_Objects/String#:~:text=JavaScript%20automatically%20converts%20primitives%20to%20String%20objects%2C%20so%20that%20it%27s%20possible%20to%20use%20String%20object%20methods%20for%20primitive%20strings). The following codepen shall present a demo. 

{% codepen https://codepen.io/mayankav/pen/OJmNPqv %}

**<u>You may have two question now</u>:**
1. If this is taken care of internally, what may go wrong if we assume that everything in JavaScript is an Object?
2. If the JS engine does this autoboxing internally everytime we use a primitive, isn't this more expensive than simply using String Objects?

To answer the first question, let me throw another example which is an extension to the very first example. The example shows a case where someone would try assigning a property to some primitive expecting it to be retrievable. If you assign some property to a string primitive with an intention of getting the value back at some point, you will only be returned '***undefined***' because the temporary String Object was discarded then and there. Similarly such assumptions can misfire [when using eval](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String?redirectlocale=en-US&redirectslug=JavaScript/Reference/Global_Objects/String#:~:text=String%20primitives%20and%20String%20objects%20also%20give%20different%20results%20when%20using%20eval()) (which indeed should not be used). These examples may not be very relatable but I guess they are enough to warn us from assumptions. 

{% codepen https://codepen.io/mayankav/pen/vYmGYpo %}

I believe the second question is no less important. Even though this seems like a tedious process of coercing a primitive into an Object and then calling the method on the temporary object, the JavaScript engine highly optimizes this process. It may even skip the creation of the extra object altogether. If you still wonder why do we even have primitives, you better ask Brendan Eich as T.J Crowder says in [this answer](https://stackoverflow.com/questions/61427733/why-is-the-string-literal-considered-a-primitive-type-in-javascript#:~:text=So%20why%20have%20primitive%20strings%3F%20You%27d%20have%20to%20ask%20Brendan%20Eich). 

<hr>

##Conclusion

To conclude I'd just highlight that MDN says "[String primitives and string objects can be used interchangeably in most situations](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String?redirectlocale=en-US&redirectslug=JavaScript/Reference/Global_Objects/String#:~:text=String%20primitives%20and%20string%20objects%20can%20be%20used%20interchangeably%20in%20most%20situations.)". We probably know why they say "**most situations**" and not "**always**". 

##Originally Posted Here -
https://mayankav.webflow.io/blog/is-everything-in-javascript-an-object