JavaScript

Primitive and Non-Primitive Data Types in JavaScript

Data types are generally known as one of the building blocks of any programming language. That is why knowing data types in a programming language is essential and crucial for becoming a professional programmer. The data types in JavaScript have been categorized into two different categories. This article will display the difference between these two different categories with the help of examples.

Additional Note: Everything in JavaScript is known to be an Object, every data type may it be primitive or non-primitive, it is an Object of JavaScript.

Primitive Data types in JavaScript

By definition, primitive data types are those data types that have been put into JavaScript by the developers of JavaScript. Or, in much simpler words, these data types have been predefined into JavaScript. The list of primitive data types of JavaScript includes the following:

All of the data types mentioned in the above list have specific constraints. These constraints include the type of value they can store inside their variable, the maximum limit of that value and the maximum size of memory they can consume. To demonstrate the working of some of the following types, look at the following examples below:

1: Strings

To create a string, simple create a variable and set it equal to a string enclosed inside the double quotation marks like:

stringVar = "Welcome To LinuxHint";

Display it on the terminal using the console log function:

console.log(stringVar);

And the terminal will show the following:

The string has been printed on the terminal, The constraint of defining a string can be the encapsulation with a double quotation mark.

2: Numbers

To create a variable of the numbers data type, simply set its value equal to an integer or a floating point like so:

num1 = 50;

num2 = 20.33;

console.log(num1);

console.log(num2);

Executing the following gives the following output on the terminal:

The constraint for creating a number is that it cannot contain any other value than numeric characters and a decimal point.

3: Undefined

Undefined is a data type not found in many programming languages. This data type simply defines the variable’s memory as assigned, but no value is placed inside that memory. To demonstrate this, use:

var x = undefined;

var y;

console.log(x);

console.log(y

One variable is set to the keyword undefined, and the other is simply created and not given any value. Upon execution of the code, the terminal shows:

Both of the variables returned undefined on the terminal.

4: Booleans & null

Booleans can be defined by creating a variable and setting them equal to the keyword true or false, and null variables can be defined by setting the value equal to the key null. Use the following lines to demonstrate both of these data types:

var x = true;

var y = null;

console.log(x);

console.log(y);

Upon running the above lines of code, the terminal displays:

Non-Primitive Data Types in JavaScript

These are the data types that the programmer defines under a particular name while writing a JavaScript program. The key point of these data types is that their size is not defined, and they can store values from almost all primitive data types. The non-primitive Data types in JavaScript are as follows:

Let’s go over the working of the non-primitive data types one by one

1: Objects

To create an object, there are two different ways, one includes the use of the “new Object()” constructor and the other is known as the literal notation. For the new Object() constructor, take the following lines:

var obj1 = new Object();

obj1.stringVal = "String inside the object";

obj1.intVal = 14;

console.log(obj1);

In the above lines:

  • The variable obj1 has been created using the new Object() constructor
  • Obj1 has been given 2 values stringVal and intVal, stringVal is a string data type value while the intVal is a number data type value.
  • The console log function is used to display the output on the terminal

Executing the above code provides the following result on the terminal:

The variable obj was displayed on the terminal in the object notation. The other way of creating an object variable is by using literal notation. To demonstrate this, use the following lines:

var obj1 = {

stringVal: "String inside the object",

intVal: 14,

};

console.log(obj1);

As it is clear from the code snippet above, to use the literal notation for object definition, simply use the curly brackets as the value of the variable and inside the curly brackets, pass the key-value pairs. Running the above lines of code provides the following output:

The output is the object’s key-value pairs in literal notation

2: Arrays

Arrays are also considered a primitive data type in JavaScript language. And the reason for this is the fact that the size of arrays is not defined, plus they can store values from primitive data types. Use the following lines as an example of array definition:

var array1 = [1, 2, true, "Google", undefined, null];

After that, pass this array variable “array1” to the console log function as:

console.log(array1);

And the result on the terminal will be:

The array1 variable with almost all primitive data types was printed on the terminal

Wrap up

Primitive data types are those data types that are predefined in JavaScript, have a constraint about how to define them and the values they can store, and last, they have a limited size. While the non-primitive data types in JavaScript include Object and Array. The non-primitive data types don’t have a constraint on the type of value they can store. Similarly, they do not have a maximum size limit on them.

About the author

Abdul Mannan

I am curious about technology and writing and exploring it is my passion. I am interested in learning new skills and improving my knowledge and I hold a bachelor's degree in computer science.