DataTypes in JavaScript


JavaScript is a loosely typed language wherein a variable can be assigned any type of value. For example,

let name = "Funny Name"; // can have string value
name = 100 // can have integer value
name = true // can have boolean

Data types in JavaScript are divided into two categories, Primitive types and Objects.

Primitive Types - Immutable Types

Primitive consists of all the data types which cannot be modified or are immutable. What does that mean, cannot be modified?

Let’s have a look at the following code,

let a = 100;

You cannot change the value 100 to 200. But you can change the value of variable a from 100 to 200. Hope it makes sense.

If it doesn’t make sense, let’s look at another example which will make it clear.

let message = "Hello World"
console.log(message[0]); // it prints H
message[0] = "G";
console.log(message); // it still prints Hello World

message is String type which is primitive type and immutable. We tried to access the zeroth character and modify it. But it cannot be modified hence immutable.

Some of the Primitive types consists of,

  • String type
  • Integer type
  • Boolean type
  • Null type

Objects - Mutable Type

Objects on the other hand is a collection of different properties and unlike primitive types are mutable.

For example, this is how an Object looks,

let obj = { 'name' : 'Samuel Johnson', 'age' : 34 };

Let’s check for mutability.

let obj = { 'name' : 'Samuel Johnson', 'age' : 34 };
obj.name = "Roy Thomson";
console.log(obj); // print {name: 'Roy Thomson', age: 34}

Now since you are clear on the two different types of data types in JavaScript. Let’s have a look at how variables are stored in memory in JavaScript.