Interactive Developers today, use various features provided by JavaScript. Along with that, most even shoot terms which may seem straight forward and similar, however, there’s distinct purpose and meaning of those terms. With this post, I’ll address the core concepts of JavaScript ensuring that the reader gets a clear picture of each term.

  • literal – represents value of specific type. For instance, in the statement below, 3.14159 and true, are literals.
var pi = 3.14159, isTrue = true; // 3.14159 and true are literals
  • primitive/ primitive type – an instance of a data type. JavaScript has five distinct primitives String, Number, Boolean, NULL, and undefined.
  • object – primitives – string, number, and boolean have constructor objects. For instance, Number, Boolean. These objects come provide means to utility methods and properties. Such as, Number provides properties like NaN, MIN_VALUE, and methods like isNan(), isFinite(), parseInt(). Ref. To elaborate further, for the following statement
var foo = 1.23;

would throw 2 in console. Since, the first statement created a number, all the properties of Number are accessible for foo. Similarly, if a string has been created, all properties of String would be accessible for the concerned variable.

 Now, this level of detail about object can’t do justice to object itself. Hence, I’ll be providing a detailed description in the upcoming post.

In JavaScript, one can create primitives in three ways –

  • direct literal assignment – when a variable is declared and value is assigned
var foo = "this is a string";
  • using literal representation – using constructor
var bar = String("this is another string");
  • using literal representation with the new keyword – using constructor with new keyword
var baz = new String("this is yet another string");

What is the difference in the above three ways? The first two are similar, only declaration method is different. However, the first two differ from the last one, as in the last one new object instance is created. In the first two, the primitive variables are strictly equal to the literals. For instance, the following code would throw true –

var foo = "this is a string";
if (foo === "this is a string"){

whereas, the following code would throw false –

var foo = new String("this is a string");
if (foo === "this is a string"){

Also, the typeof method would throw object for the one with new keyword.

While working on a project, majority of times developers would not require to create an object of primitives, instead direct primitive would be created to serve the purpose.

This is the first post for JavaScript 101 course. I’ll be coming up with next post soon. Do provide your feedback.