JavaScript Data Types
5 April 2025 | Category: JavaScript
In JavaScript, data types represent the kinds of values we can work with and store in variables. Knowing them is essential to write bug-free code.
JavaScript has two main categories of data types:
đź“‚ 1. Primitive Data Types
These are basic, immutable (unchangeable) data types.
Data Type | Description |
---|---|
String | Text data |
Number | Any numeric value |
Boolean | True or False |
Undefined | Variable declared but not assigned a value |
Null | Intentionally empty value |
Symbol (ES6) | Unique and immutable value |
BigInt (ES11) | Large integers beyond Number limit |
đź§Ş 1. String
– Text values
let name = "John";
let message = 'Hello';
let sentence = `Welcome, ${name}`;
- Strings can be in “double quotes”, ‘single quotes’, or
template literals
(backticks). - Template literals support string interpolation using
${}
.
đź§® 2. Number
– Numeric values
let age = 25;
let price = 99.99;
let negative = -10;
- Includes both integers and floating-point numbers.
- JavaScript uses only one type for numbers:
number
.
âś… 3. Boolean
– Logical values
let isActive = true;
let isLoggedIn = false;
- Only two possible values:
true
orfalse
. - Common in conditions (
if
,while
, etc.).
âť“ 4. Undefined
– Uninitialized variable
let x;
console.log(x); // Output: undefined
- A variable declared but not assigned gets
undefined
.
đźš« 5. Null
– Empty value
let data = null;
- Represents “no value” or “empty” on purpose.
- Useful when you want to clear a variable manually.
đź§ż 6. Symbol
– Unique identifiers (ES6)
let id = Symbol("userID");
- Used to create unique keys in objects, even if values are the same.
- Mostly used in advanced object handling and libraries.
đź’Ż 7. BigInt
– Very large integers (ES11)
let bigNumber = 1234567890123456789012345678901234567890n;
- Can store numbers larger than
2^53 - 1
. - Add
n
at the end to declare a BigInt.
đź§± 2. Non-Primitive (Reference) Data Types
These are more complex and store references (not actual values).
Type | Description |
---|---|
Object | Collection of key-value pairs |
Array | Ordered list of values |
Function | Reusable block of code |
đź§° 1. Object
– Group of key-value pairs
let person = {
name: "Alice",
age: 30,
isStudent: false
};
- Values are accessed using
object.key
orobject["key"]
.
đź“‹ 2. Array
– List of values
let colors = ["red", "green", "blue"];
console.log(colors[0]); // Output: red
- Arrays use indexes starting from
0
. - Can store mixed data types too:
let mixed = [1, "text", true];
⚙️ 3. Function
– Block of reusable code
function greet(name) {
return `Hello, ${name}`;
}
- Functions are also objects in JavaScript and can be passed as arguments.
🔍 Checking Data Types
Use the typeof
operator:
console.log(typeof "Hello"); // string
console.log(typeof 42); // number
console.log(typeof true); // boolean
console.log(typeof null); // object (quirk in JavaScript)
console.log(typeof undefined); // undefined
console.log(typeof []); // object (Arrays are objects)
console.log(typeof {}); // object
console.log(typeof function(){}); // function
đź§ Quick Summary
Data Type | Example |
---|---|
String | "Hello" , 'Hi' |
Number | 123 , 3.14 , -99 |
Boolean | true , false |
Undefined | let x; |
Null | let x = null; |
Symbol | Symbol("id") |
BigInt | 1234567890n |
Object | { name: "John" } |
Array | [1, 2, 3] |
Function | function greet() {} |