1. JavaScript Data Types
🧠JavaScript Data Types
What is a Data Type?
-
A data type tells JavaScript what kind of value a variable holds.
-
A variable is a container that stores a value — like a number, text, or object.
-
Data types help programs understand how to use that data.
🧩 Basic Data Types in JavaScript
1. Number
-
Used for integers and decimal (floating-point) numbers.
let age = 25;
const pi = 3.14; -
Examples:
7,19,90,3.14,5.2
2. String
-
Represents text, written inside quotes.
let greeting = "Hello, world";
let language = 'JavaScript';
3. Boolean
-
Represents true or false values.
let isLoading = false;
let isLoggedIn = true;
4. undefined
-
A variable declared but not given a value yet.
let name;
console.log(name); // undefined
5. null
-
A variable that is intentionally set to nothing.
let user = null;
6. Object
-
A complex data type that stores key-value pairs.
let book = {
title: "The Great Gatsby",
author: "F. Scott Fitzgerald",
year: 1925
}; -
Keys = property names (
title,author,year) -
Values = data they hold (
"The Great Gatsby", etc.) -
Each key-value pair is called a property.
7. Symbol
-
A unique and immutable (unchangeable) value.
const symbol1 = Symbol('mySymbol');
const symbol2 = Symbol('mySymbol');
console.log(symbol1 === symbol2); // false
8. BigInt
-
Used for very large numbers that go beyond Number’s limit.
const bigNumber = 1234567890123456789012345678901234567890n;
console.log(bigNumber);
🧾 Summary
| Data Type | Example | Description | Real-World Use |
|---|---|---|---|
| Number | let price = 499.99; |
Represents numeric values (integers or decimals) | Tracking a product’s price, temperature, or age |
| String | let name = "John Doe"; |
Text or sequence of characters | Storing usernames, messages, or display text |
| Boolean | let isOnline = true; |
Logical value: true or false |
Checking if a user is logged in, dark mode on/off |
| undefined | let phone; |
Declared but not assigned a value | Variable waiting for user input or API data |
| null | let selectedItem = null; |
Empty on purpose — no value yet | Resetting variables or clearing form fields |
| Object | let user = { name: "Liya", age: 20 }; |
Group of related data (key-value pairs) | Representing user profiles, products, cars, etc. |
| Symbol | const token = Symbol('id'); |
Unique identifier, never duplicated | Creating private keys or unique property names |
| BigInt | let bigNum = 98765432109876543210n; |
Very large integers beyond normal Number limit | Handling financial data, scientific calculations |