DEV Community

shaikh_md
shaikh_md

Posted on

The DataTypes of JavaScript

Data Types

In the previous section, we mentioned a little bit about data types. Data or values have data types. Data types describe the characteristics of data. Data types can be divided into two:

  1. Primitive data types
  2. Non-primitive data types(Object References)

Primitive Data Types

Primitive data types in JavaScript include:

  1. Numbers - Integers, floats
  2. Strings - Any data under single quote, double quote or backtick quote
  3. Booleans - true or false value
  4. Null - empty value or no value
  5. Undefined - a declared variable without a value
  6. Symbol - A unique value that can be generated by Symbol constructor

Non-primitive data types in JavaScript includes:

  1. Objects
  2. Arrays

Now, let us see what exactly primitive and non-primitive data types mean.
Primitive data types are immutable(non-modifiable) data types. Once a primitive data type is created we cannot modify it.

Example:

let word = "Love JavaScript";
Enter fullscreen mode Exit fullscreen mode

If we try to modify the string stored in variable word, JavaScript should raise an error. Any data type under a single quote, double quote, or backtick quote is a string data type.

word[0] = "Y";
Enter fullscreen mode Exit fullscreen mode

This expression does not change the string stored in the variable word. So, we can say that strings are not modifiable or in other words immutable.
Primitive data types are compared by its values. Let us compare different data values. See the example below:

let numOne = 3;
let numTwo = 3;

console.log(numOne == numTwo); // true

let js = "JavaScript";
let py = "Python";

console.log(js == py); //false

let lightOn = true;
let lightOff = false;

console.log(lightOn == lightOff); // false
Enter fullscreen mode Exit fullscreen mode

Non-Primitive Data Types

Non-primitive data types are modifiable or mutable. We can modify the value of non-primitive data types after it gets created.
Let us see by creating an array. An array is a list of data values in a square bracket. Arrays can contain the same or different data types. Array values are referenced by their index. In JavaScript array index starts at zero. I.e., the first element of an array is found at index zero, the second element at index one, and the third element at index two, etc.

let nums = [1, 2, 3];
nums[0] = 10;

console.log(nums); // [10, 2, 3]
Enter fullscreen mode Exit fullscreen mode

As you can see, an array, which is a non-primitive data type is mutable. Non-primitive data types cannot be compared by value. Even if two non-primitive data types have the same properties and values, they are not strictly equal.

let nums = [1, 2, 3];
let numbers = [1, 2, 3];

console.log(nums == numbers); // false

let userOne = {
  name: "shaikh_md",
  role: "learning",
  country: "IN",
};

let userTwo = {
  name: "shaikh_md",
  role: "learning",
  country: "IN",
};

console.log(userOne == userTwo); // false
Enter fullscreen mode Exit fullscreen mode

Rule of thumb, we do not compare non-primitive data types. Do not compare arrays, functions, or objects.
Non-primitive values are referred to as reference types, because they are being compared by reference instead of value. Two objects are only strictly equal if they refer to the same underlying object.

let nums = [1, 2, 3];
let numbers = nums;

console.log(nums == numbers); // true

let userOne = {
  name: "shaikh_md",
  role: "learning",
  country: "IN",
};

let userTwo = userOne;

console.log(userOne == userTwo); // true
Enter fullscreen mode Exit fullscreen mode

If you have a hard time understanding the difference between primitive data types and non-primitive data types, you are not the only one. Calm down and just go to the next section and try to come back after some time. Now let us start the data types by number type.

Top comments (0)