Primitives vs Objects in Javascript

Piyush Sinha
The MavenHive Blog
Published in
2 min readMay 31, 2018

Values in JavaScript are of two types: Primitives & Objects. Yeah you read it correctly, not everything in JavaScript is an Object.

What are primitives?
The following values are primitives
* Strings: "abc"
* Numbers: 1, 2, 3.57 (All numbers are floating point)
* Boolean: true or false
* null
* undefined

Everything else is an object
*
Created by literals. Literals produce objects that can be created via a constructor. We should use literals
->[] is samethe as new Array()
->{} is same as new Object()
->function() {} is same as new Function()
* Dates: new Date(“2011–12–24”)
* Wrappers for primitives: Boolean, Number, String.

Lets understand how primitives and objects differ by understanding their nature.

  1. Objects are mutable.
> let obj = {};
> obj.foo = "123"; //write
"123"
> obj.foo; //read
"123"

Primitives are immutable. Any property you add will be forgotten immediately.

> let str = "abc";
> str.foo = 123; // write - ignored
123
> str.foo // read
undefined

2. Objects have unique identities and are compared by reference.Every object you create is different. Two objects will be only equal if they have same identity. It does not matter if they have same content or not. The fact can be determined by === operator.

> {} === {}
false
> let obj = {};
> obj === obj
true

Primitives are compared by value, they don’t have individual identities. For primitives if there values are equal they are considered equals.

> "foo" === "foo"
true

Primitive Values and their Wrappers

Please avoid wrapper types as much as possible.
The three primitive types string, number and boolean have corresponding types whose instances are objects: String, Number, Boolean. They are sometimes called wrapper types and converting between primitive and wrapper is simple:

  • Primitive to wrapper: new String(“abc”)
  • Wrapper to primitive: new String(“abc”).valueOf()

Primitive Values such as “abc” are fundamentally different than wrapper instances new String(“abc”) . We will notice that in examples below.

> typeof "abc"
'string'
> typeof new String("abc")
'object'
> "abc" instance of String
false
> new String("abc") instance of String
true
> "abc" === new String("abc")
false

Primitive Values have no methods of their own

Primitives don’t have their own methods and they borrow them from wrappers

> "abc".charAt === String.prototype.charAt
true

There are two ways borrowing is done:

  1. Convert the primitive to a wrapper (Old Way).
  2. Transparently use the method from wrappers prototype
// Methods in Object.prototype are available to all primitives
Object.prototype.getType = function() {
return typeof this;
};
Object.prototype.getTypeStrict = function() {
"use strict";
return typeof this;
};
console.log("".getType()); // object
console.log("".getTypeStrict()); // string

References:
Thanks to Dr. Axel Rauschmayer for his wonderful posts about Javascript world.

--

--