JavaScript Data Types
Every value in JavaScript has a type. A data type tells JavaScript what kind of value is stored in a variable and what operations can be performed on it. For example, a number can be added or multiplied, while a piece of text (string) can be joined with other text.
JavaScript has two categories of data types: Primitive and Non-Primitive (Reference).
Primitive Data Types
Primitive types store a single, simple value. There are seven primitive types in JavaScript.
1. String
A string is a sequence of characters used to represent text. Strings are written inside single quotes ' ', double quotes " ", or backticks ` `.
let firstName = "Aman";
let city = 'Delhi';
let greeting = `Hello, World!`;
console.log(firstName); // Aman
console.log(typeof firstName); // string2. Number
JavaScript uses a single Number type for all numeric values — whether whole numbers or decimals.
let age = 25;
let price = 99.99;
let temperature = -5;
console.log(age); // 25
console.log(typeof age); // numberJavaScript also has two special number values:
Infinity— Result of dividing a number by zeroNaN— "Not a Number" — result of an invalid numeric operation
console.log(10 / 0); // Infinity
console.log("hello" * 2); // NaN3. Boolean
A boolean has only two possible values: true or false. Booleans are the result of comparisons and are heavily used in decision-making (if/else statements).
let isLoggedIn = true;
let hasPermission = false;
console.log(isLoggedIn); // true
console.log(typeof isLoggedIn); // boolean
let result = 10 > 5;
console.log(result); // true4. Undefined
A variable that is declared but not yet assigned a value automatically holds the value undefined.
let address;
console.log(address); // undefined
console.log(typeof address); // undefined5. Null
null represents an intentional absence of value. It is used when a variable is explicitly set to "nothing" or "empty".
let selectedItem = null;
console.log(selectedItem); // null
console.log(typeof selectedItem); // object (this is a known quirk in JS)null vs undefined: undefined means a variable exists but has no value yet. null means the developer intentionally set it to nothing.
6. BigInt
BigInt is used for very large integers that exceed the safe limit of the Number type. It is created by appending n to an integer.
let bigNumber = 9007199254740991n;
console.log(bigNumber); // 9007199254740991n
console.log(typeof bigNumber); // bigint7. Symbol
Symbol creates a unique, immutable identifier. It is mainly used in advanced programming patterns to avoid naming conflicts.
let id = Symbol("userID");
console.log(id); // Symbol(userID)
console.log(typeof id); // symbolNon-Primitive (Reference) Data Types
Non-primitive types can hold multiple values and are more complex structures.
1. Object
An object is a collection of related data stored as key-value pairs. Objects represent real-world entities like a person, car, or product.
let student = {
name: "Pooja",
age: 20,
course: "JavaScript"
};
console.log(student.name); // Pooja
console.log(student.age); // 20
console.log(typeof student); // object2. Array
An array is an ordered list of values. Arrays can hold any type of data — numbers, strings, booleans, even other arrays or objects.
let fruits = ["Apple", "Banana", "Mango"];
console.log(fruits[0]); // Apple
console.log(fruits.length); // 3
console.log(typeof fruits); // object (arrays are a type of object)3. Function
Functions are reusable blocks of code. In JavaScript, functions are also considered a data type because they can be stored in variables, passed as arguments, and returned from other functions.
function greet(name) {
return "Hello, " + name;
}
console.log(typeof greet); // functionThe typeof Operator
The typeof operator is used to check the data type of any value or variable.
console.log(typeof "Hello"); // string
console.log(typeof 42); // number
console.log(typeof true); // boolean
console.log(typeof undefined); // undefined
console.log(typeof null); // object ← known JS quirk
console.log(typeof {}); // object
console.log(typeof []); // object
console.log(typeof function(){}); // functionDynamic Typing in JavaScript
JavaScript is a dynamically typed language. This means a variable can hold any type of value, and the type can change during the program's execution.
let data = 42;
console.log(typeof data); // number
data = "now I am a string";
console.log(typeof data); // string
data = true;
console.log(typeof data); // booleanThis flexibility is powerful but can also lead to bugs if types change unexpectedly. Careful coding practices help avoid such issues.
Primitive Types Summary Table
| Type | Example | typeof Result |
|---|---|---|
| String | "Hello" | string |
| Number | 42, 3.14 | number |
| Boolean | true, false | boolean |
| Undefined | undefined | undefined |
| Null | null | object |
| BigInt | 100n | bigint |
| Symbol | Symbol("id") | symbol |
Practical Example
// Representing a product in an online store
const productName = "Wireless Headphones"; // string
const price = 1299.99; // number
const inStock = true; // boolean
const discount = null; // null (no discount currently)
let rating; // undefined (not yet rated)
console.log("Product:", productName);
console.log("Price:", price);
console.log("Available:", inStock);
console.log("Discount:", discount);
console.log("Rating:", rating);Key Points to Remember
- JavaScript has 7 primitive types: String, Number, Boolean, Undefined, Null, BigInt, Symbol
- Non-primitive types include Object, Array, and Function
- Use
typeofto check the type of any value typeof nullreturns "object" — this is a known JavaScript quirk- JavaScript is dynamically typed — a variable's type can change
undefinedmeans a variable has no value;nullmeans it is intentionally empty
