What is Datatypes in JavaScript ?

What is Datatypes in JavaScript ?

Understanding JavaScript Datatypes: A Real-Life Example Approach

·

3 min read


What is a datatype, and why is it called a datatype? Don’t worry—I’ll explain it clearly with a real-life example so you can understand. Imagine you’ve gone to a digital store to buy a laptop for gaming and programming. The staff present several options: an old Acer laptop, an Asus ROG, a MacBook Air, and an Asus VivoBook.

The staff explain that the Acer laptop is suitable for learning and study purposes. They describe the Asus ROG as ideal for learning, programming, hardcore gaming, and running powerful software. The MacBook Air is recommended for editing and programming, with a focus on speed. Finally, they present the VivoBook as best for office-related and casual tasks.

In this analogy, each laptop represents a different datatype, tailored for specific uses. Similarly, in programming, a datatype defines the kind of data a variable can hold and how it can be used.

So when it comes to JavaScript we have two types of datatypes , They are as follows :

Primitive & Non Primitive Datatypes

Non primitive datatypes are also called as reference datatypes which i will describe it later.

The primitive datatypes are

Number = 20

Null = empty value

Boolean = true/false , but not both

bigInt = 102736363636n bigger mathematical values

String = "Aadarsh"

Symbol =unique values

undefined = the value is not defined or not initialized yet

Why it is called as primitive datatypes ? Ever wondered ? Because when ever we do anything with these operator like when we assign the same value to a different variable like

const url = "shutthefvckup.com";

const linkUrl = url;

So here the variable url is declared first and we assigned the corresponding value and secondly we created an another variable called linkUrl and assigned the same value, here the value is not directly gone to the second variable the js just send a copy of the variable to the linkUrl variable. So whatever we do with the linkUrl variable the changes will not affect the main url variable, Because the linkUrl variable contains a copy of url variables value which is "shutthefvckup.com"

Non Primitive Datatypes are as follows :

Objects = { position : "Senior Soap Developer" , age : 20} Collections of key-value pairs.

Functions = function sayMyName( ){

const name = "Walter White";

return My name is ${name}

} Blocks of code that can be invoked.

Arrays = [1,2,3,4,5,6,7,8,9,10]; Ordered lists of values.

So why it is called as a reference datatypes ? Don't worry i can simplify.

lets store some data inside a variable called as user.

const user = { username: "john_doe", password: "securePassword123", age: 30 };

and lets make an another variable called as user2 and assign the value same as the user variable.

const user2 = user;

So now we think , the value passed here is same as the primitive datatype values, But its actually no the case.

The value passed to the user2 is not a copy of user variable it is shares the same reference which is user variable. That means whenever we made any changes to the user2 variable the original value gets manipulated.

lets see.

const user = { username: "john_doe", password: "securePassword123", age: 30 };

const user2 = user; user2.username = "tyler duren";

console.log(user) => // this will be the output { username: 'tyler duren', password: 'securePassword123', age: 30 }

console.log(user2) // this will be the output { username: 'tyler duren', password: 'securePassword123', age: 30 }

See the original value gets manipulated. That's it ,I hope was helpful.

Thanks