JavaScript Tutorial
JavaScript Data Types
JavaScript data types describe the kind of value you can store in a variable. Understanding data types is important because they help you work with numbers, text, true/false values, objects, and more.
JavaScript has two main categories of data types:
- Primitive Data Types
- Non-Primitive (Reference) Data Types
Let’s explore them in simple terms.
1. Primitive Data Types
Primitive types are simple and cannot be changed once created (they are immutable). JavaScript has 7 primitive data types.
1.1 Number
Represents numeric values such as integers and decimals.
Example
let age = 25;
let price = 99.99;
1.2 String
Represents text and characters. You can use single quotes, double quotes, or backticks.
Example
let name = "learn2kode";
let city = 'Chennai';
let message = `Welcome to Learn2Kode`;
1.3 Boolean
Represents true or false.
Example
let isLoggedIn = true;
let isPaid = false;
1.4 Undefined
A variable that is declared but not assigned a value.
Example
let value;
console.log(value); // undefined
1.5 Null
Represents empty or no value. You assign it manually.
Example
let data = null;
1.6 BigInt
Used for very large numbers beyond JavaScript’s normal limit.
Example
let bigNumber = 12345678901234567890123456789n;
1.7 Symbol
Used to create unique identifiers, helpful in advanced JavaScript.
Example
let id = Symbol("userID");
2. Non-Primitive (Reference) Data Types
These are mutable and store collections or complex data.
2.1 Object
An object stores key–value pairs.
Example
let user = {
name: "learn2kode",
age: 25,
city: "Chennai"
};
2.2 Array
A list of values, stored inside square brackets.
Example
let colors = ["red", "blue", "green"];
2.3 Function
Functions themselves are considered data types.
Example
function greet() {
console.log("Hello!");
}
Summary Table
How to Check Data Type
Use the typeof operator:
console.log(typeof "Hello"); // string
console.log(typeof 25); // number
console.log(typeof true); // boolean
console.log(typeof {}); // object
console.log(typeof []); // object (arrays are objects)
console.log(typeof null); // object (JavaScript bug)
Important Notes
Use the typeof operator:
- null returns "object" → known JavaScript bug
- Arrays return "object" → because they are special objects
- Functions return "function" but are still objects internally