DEV Community

Amit Mondal
Amit Mondal

Posted on

Everything in JavaScript is not an Object

It is a very common misbelief or misstatement that "everything in JavaScript is an object" in the JavaScript world. Objects definitely are the general building blocks in which JavaScript is built on but that does not mean that everything is an object.

JavaScript has two main kinds of values: Primitive and Object. Primitive values are immutable whereas Objects are mutable.

The following values are primitive:

  • string: "Amit"
  • number: 10, 3.57 (all numbers in JavaScript are floating-point)
  • boolean: true, false
  • null: Assignment value that means "no value". Usually explicitly assigned. Many of us might have encountered typeof null returning "object" but this is a well-known bug in JavaScript that can’t be fixed because it would break existing code. That does not mean that null is actually an object
  • undefined: Javascript sets unassigned variables with a default value of undefined.

All other values are objects. Objects can be partitioned further:

  • Wrappers for primitives: Boolean, Number, String. Rarely used directly.
  • Creatable by literals. The following literals produce objects that can also be created via a constructor. Use literals whenever you can.
    • Array: [] is the same as new Array()
    • Object: {} is the same as new Object()
    • Function: function() {} is the same as new Function()
    • RegExp: /\s*/ is the same as new RegExp("\s*")
    • Date: new Date("2011-12-24")
    • Error: Usually you create an Error object with the intention of raising it using the throw keyword. throw new Error('Whoops!').

These built-ins have the appearance of being actual types or even classes. But in JavaScript, these are actually just built-in functions.

var strPrimitive = "This is a string";
typeof strPrimitive;  // "string"
strPrimitive instanceof String;  // false

var strObject = new String("This is a string");
typeof strObject;  // "object"
strObject instanceof String;  // true
Enter fullscreen mode Exit fullscreen mode

In the above code-snippet the primitive value "This is a string" is not an object, it's a primitive literal and immutable value. To perform operations on it, such as checking its length, accessing its individual character contents, etc, a String object wrapper is required.

Luckily, the JavaScript automatically coerces a "string" primitive to a String object wrapper when necessary, which means you almost never need to explicitly create the Object form.

Now consider,

var strPrimitive = "My name is Amit";

console.log( strPrimitive.length ); //15

console.log( strPrimitive.charAt( 11 ) ); //"A"
Enter fullscreen mode Exit fullscreen mode

In both the console statements, we call a property or method on a string primitive, and the engine automatically coerces it to a String object, so that the property/method access works. Similarly, we have object wrapper for number and boolean. null and undefined have no object wrapper form, only their primitive values.

So going forward if someone says "everything in javascript is an object" just politely explain to them why that's not true or just share this post with them.
But yes you can always say that "Most of them are objects but not everything"

I would highly recommend everyone to read You Don't Know JS by Kyle Simpson. Before reading this book I was one of them who thought "Everything in JS is an Object".

Thank you for reading.

Latest comments (1)

Collapse
 
pavancse17 profile image
Pavan Kumar

Nice post