JavaScript Data Types

Introduction

Understanding JavaScript data types is a fundamental knowledge to understanding shallow copy, deep copy and many other functionalities in JavaScript.

Data Types in JavaScript

JavaScript data types are of two major kinds:

  • The primitive data type, and
  • The structural data type.

The primitive data types include:

  1. Undefined
  2. Boolean
  3. Number
  4. String
  5. BigInt
  6. Symbol

The structural data types include:

  1. Object – which also include:

    • (new) Object,
    • Array,
    • Map,
    • Set,
    • Weak Map,
    • Date.
  2. Function

Characteristics of JavaScript Data types kinds in JavaScript.

As explained earlier that JavaScript fundamentally have two kind of data types, which are Primitive data types, and structural data types. These data type kinds have their characteristics which make one different from the other.

  1. Primitive data types pass value. What does this mean, you would say?

Let’s take an example.

Let’s say you have these:

let x = 2;
let y = x
y +=1

console.log(x)
console.log(y)

What do you think the results of this would be?

Yes. The value for x still remains the same while the value of y incremented as we define in our JavaScript code. This is because, the value of x variable is part of the primitive data type, a number, and it will therefore pass a value, which in our case is

Structural data types on the other handpass references. What does this mean?

Let’s take an example.

let xArray = [1,2,3] let yArray = xArray yArray.push(4)

console.log(xArray) console.log(yArray)

What do you think the results would be? In this case, you will realize that both the values of xArray and yArray got changed; this is because the variables xArray and yArray are containing data types that are part of the primitive data types. Therefore both variables will be references to a memory location where the data, in our case, an Array is stored.

Quick Question: what are primitive data types? Answer here