Recent Post:
Categories:
Explanation:
In JavaScript, a "data type" is the kind of information you're working with. It's like how you have different types of things in real life—like a book, a toy, or a pencil. Each one is different and used in different ways.
In JavaScript, there are Primitive Data Types (simple ones) and Non-Primitive Data Types (more complex ones).
1. String: Think of this as words or text. You write it in quotes.
code
let name = "Satyendra";
Here, "Satyendra" is a string.
2. Number: These are just numbers, like how you count things.
code
let age = 25;
25 is a number.
3. BigInt: This is for super big numbers that normal numbers can't handle.
code
let bigNumber = 12345678901234567890n;
The n at the end makes it a bigint.
4. Boolean: This is either true or false, like a light switch that can be ON (true) or OFF (false).
code
let isStudent = true;
true is a boolean.
5. Undefined: This means you haven’t put anything in the box yet—it’s empty.
code
let job;
job is undefined because we haven’t put anything in it yet.
6. Null: This is an empty value, meaning "nothing is here."
code
let car = null;
car is null because we’ve said there’s nothing in it.
code
let name = "Alice"; // string
let age = 10; // number
let isHappy = true; // boolean
let largeNumber = 123456789n; // bigint
let color; // undefined
let favoriteFood = null; // null
We have six variables: name is a string holding the value "Alice." age is a number with the value 10. isHappy is a boolean with the value true, meaning yes. largeNumber is a really big number (bigint). color is undefined because we didn’t give it a value yet. favoriteFood is null, which means there’s nothing in that box for now.
Object: This is more complex—it can hold many different things inside, like a backpack with different items.
code
let person = {
name: "Alice",
age: 10,
isStudent: true
};
Here, person is an object with three pieces of information: name, age, and isStudent.
code
let car = {
brand: "Toyota",
model: "Corolla",
year: 2022,
isElectric: false
};
car is an object. Think of it like a backpack that holds many things: The brand is "Toyota." The model is "Corolla." The year is 2022. And isElectric is false, meaning the car is not electric.
Objects are used when you want to group lots of information together in one place.