Learning javascript – Object, primitive and literal

By | July 18, 2016

While programming ever wondered what is difference between object, primitive and literal  in javascript? or while reading some article we often get confused among the three. In this article I will cover the difference between the three. Let’s see what all the three mean in javascript programming.

Become an Expert in Programming and Web Development.View all courses!

Before going further you may want to learn about data types in javascript here.

primitive

Literal, Object and Primitive

JavaScript Literal

JavaScript literal represents value of a specific data type, such as quoted string, floating point number or boolean.

In the first case I have created a string literal. Next is float literal and the last is boolean. Here we have used the literal notation to use the extensive set of functionality. However if we want to do more with variable we have to use the data type’s complementary  javascript object and its properties.

For example, if I want to calculate the  length of a string as shown in the above code, we will access the String object’s length property.

While calculating the length a new string object is created. This new string object is created by accessing the String object’s property on the literal. And the value of this new object is set to the str1’s values. The length property is accessed and printed out and the newly created object is discarded.

This is the basic difference between creating a literal and String object using new keyword as discussed in the next section. If you are planning to use any of the functions on the primitive types it’s better to use the object notation of the primitive type. This will prevent from conversions which I discussed in the previous para.

JavaScript primitive

There are exactly five primitive data types in javascript. String, number, boolean, null and undefined. Only string, number and boolean data types have alternate constructor object. Otherwise these three primitives are represented as literals as shown in the code below.

We can create the primitive boolean, string and number variables either by using a literal representation as in the above code. Or it can be created using the object without using the new operator.

Objects in Javascript

To create an object we have to use the new keyword as shown in below code

We should note sown here that the javascript developers don’t directly create object instances for the three primitive data types.Developers just want a number boolean or string to just act like number, boolean or string rather than an object.

Strict Comparison:

We use “===” operator to check the object type equality in javascript. For == and === operator you can visit this article.

Can you spot the difference between above two code snippets. In the first case the output will be “equal” as on both side of the equality operator, the operands are of type string.

But in the second case one is type object i.e. str1 and the other is string literal. The result will be “Not Equal”.

Conclusion:

In this article I have discussed all the three very confusing words literals, primitives and objects. If these words are not clearly understood learning javascript can become difficult. But on the other hand if we are very much known to these words it is very easy to learn javascript.