JavaScript Data Types Explained with Examples
JavaScript is a dynamically typed language, which means you don’t have to specify the data type of a variable when declaring it. The type is determined automatically based on the value assigned to it.
JavaScript has eight data types, divided into two main categories:
- Primitive Data Types
- Non-Primitive (Reference) Data Types
1. Primitive Data Types
Primitive data types represent a single value and are immutable (cannot be changed).
The primitive types are:
- String
- Number
- Boolean
- Undefined
- Null
- Symbol
- BigInt
Let’s understand each one in detail.
1.1 String
A string is a sequence of characters used to represent text.
Strings can be enclosed in single quotes ('), double quotes ("), or backticks (`).
Example:
let firstName = "John";
let message = 'Welcome to JavaScript!';
let greeting = `Hello, ${firstName}!`; // Template literal
console.log(firstName); // John
console.log(message); // Welcome to JavaScript!
console.log(greeting); // Hello, John!
console.log(typeof firstName); // string
1.2 Number
The number type is used for both integers and floating-point numbers. JavaScript does not distinguish between integer and decimal values.
Example:
let age = 25;
let price = 99.5;
let temperature = -10;
console.log(age + price); // 124.5
console.log(typeof price); // number
1.3 Boolean
A boolean type represents only two possible values: true or false.
It is commonly used for decision-making and condition checking.
Example:
let isOnline = true;
let isVerified = false;
console.log(isOnline); // true
console.log(typeof isOnline); // boolean
if (isOnline) {
console.log("User is online");
}
1.4 Undefined
A variable that has been declared but has not been assigned a value is of type undefined.
Example:
let city;
console.log(city); // undefined
console.log(typeof city); // undefined
1.5 Null
The null type represents the intentional absence of any value. It must be assigned manually by the developer.
Example:
let user = null;
console.log(user); // null
console.log(typeof user); // object (this is a known JavaScript bug)
1.6 Symbol
Introduced in ES6, the symbol type is used to create unique identifiers. Symbols are often used as object keys to avoid name conflicts.
Example:
let id1 = Symbol("id");
let id2 = Symbol("id");
console.log(id1 === id2); // false
console.log(typeof id1); // symbol
1.7 BigInt
The BigInt type allows working with very large integers beyond the limit of the Number type (which is 2^53 - 1).
Example:
let bigNumber = 1234567890123456789012345678901234567890n;
console.log(bigNumber);
console.log(typeof bigNumber); // bigint
2. Non-Primitive (Reference) Data Types
Non-primitive data types can store multiple values and are mutable. They include objects, arrays, and functions.
2.1 Object
An object is a collection of key-value pairs. Each key is a property, and each property has a value.
Example:
let student = {
name: "Alice",
age: 22,
course: "JavaScript"
};
console.log(student.name); // Alice
console.log(typeof student); // object
2.2 Array
An array is a special type of object used to store an ordered list of values.
Example:
let fruits = ["Apple", "Banana", "Mango"];
console.log(fruits[0]); // Apple
console.log(typeof fruits); // object
2.3 Function
A function is an object that can be invoked to perform a specific task.
Example:
function greet(name) {
return "Hello, " + name + "!";
}
console.log(greet("John")); // Hello, John!
console.log(typeof greet); // function
Summary Table
| Data Type | Category | Example | typeof Result |
|---|---|---|---|
| String | Primitive | "Hello" |
“string” |
| Number | Primitive | 45, 3.14 |
“number” |
| Boolean | Primitive | true |
“boolean” |
| Undefined | Primitive | let x; |
“undefined” |
| Null | Primitive | let x = null; |
“object” |
| Symbol | Primitive | Symbol("id") |
“symbol” |
| BigInt | Primitive | 123n |
“bigint” |
| Object | Non-Primitive | {name: "John"} |
“object” |
| Array | Non-Primitive | ["a","b","c"] |
“object” |
| Function | Non-Primitive | function() {} |
“function” |
Live Example: Testing All Data Types Together
You can try this code in your browser console:
let name = "Sara"; // String
let age = 28; // Number
let isStudent = true; // Boolean
let score; // Undefined
let car = null; // Null
let uniqueKey = Symbol("id"); // Symbol
let bigValue = 9007199254740991n; // BigInt
let person = { name, age }; // Object
console.log(typeof name, name);
console.log(typeof age, age);
console.log(typeof isStudent, isStudent);
console.log(typeof score, score);
console.log(typeof car, car);
console.log(typeof uniqueKey, uniqueKey);
console.log(typeof bigValue, bigValue);
console.log(typeof person, person);
Your Feedback
Help us improve by sharing your thoughts
Online Learner helps developers master programming, database concepts, interview preparation, and real-world implementation through structured learning paths.
Quick Links
© 2023 - 2026 OnlineLearner.in | All Rights Reserved.
