Data Types in JavaScript

In the JavaScript, there is a bunch of different type of data types but there are basically two types of data types and that's Primitive and Reference.

  • With the Primitive data type, the data is stored directly in the location that the variable accesses it's stored on the stack when you access primitive data you access it by its actual value.

  • In JavaScript, we are having six Primitive Data Types


The following are the different types of data types-

  • String (which are sequences of characters, they can be letters numbers symbols whatever)
//String Type
const name= "John Carter"

console.log(typeOf name)

In the console, we could clearly see its string type.

  • Number (It is just a number, it's important to know that integers, decimals, floats are considered to be numbers in JavaScript which makes it much easier)
//Number Type
const numb= 50;

console.log(typeOf numb)

In the console, we could see the variable is of number data type

  • Boolean (we also have a Boolean type which is true or false)
//Boolean Type
const hasKids= true;

console.log(typeOf hasKids)

In the console we could see the variable holds boolean type ;

  • Null (We have NULL which is an intentional empty value)
//Null Type
const car= null;

console.log(typeOf car)

In the console, we get the object.

- Undefined (we have undefined which is a variable that has not been assigned a value, all variables are undefined by default)

//Undefined Type
let test;

console.log(typeOf test)

In the console, we get undefined as an output.

- Symbols(ES6, Its a new primitive data type)


const sym = Symbol();
console.log(typeof sym)

In the console, we get a symbol as an output.


Reference data types are objects that are accessed by reference. So the data isn't actually stored in the particular variable it's stored on what's called the heap which has to do with dynamically allocated memory. Reference types are not primitive, they are not accessed by the actual value they are accessed by a reference and they are also considered as objects.

- Arrays - Object Literals - Functions - Dates - Anything Else

So Arrays, Objects, Literals, Functions, Dates and anything else you can store is gonna be a reference type. So now let's take a look at a couple of reference types, and these are going to all come back as objects.

//Array
const hobbies = ["movies", "playing"];

console.log(typeof hobbies);

In the console, we get an object as an output.


So JavaScript is what's known as dynamically typed language and this means that the data types are associated with the actual values and not the variables themselves. So the same variable can hold multiple types meaning it can hold a string and then it can be set to a number with no errors or issues and it's absolutely fine.


Did you find this article valuable?

Support Sarvesh's Blog by becoming a sponsor. Any amount is appreciated!