What are Data Types in JavaScript?
In JavaScript, data types define the type of value a variable can hold. Understanding data types is crucial because they determine what kind of operations can be performed on the data.
Categories of Data Types
JavaScript has two main categories of data types:
- Primitive Data Types: String, Number, BigInt, Boolean, null, undefined, Symbol
- Reference (Non-Primitive) Data Types: Objects, Arrays, Functions
Primitive Data Types Explained
1. String
Represents textual data. Strings can be enclosed in single, double, or backtick (template) quotes.
let name = 'Alice';
let greeting = "Hello";
let template = `Hi, ${name}!`;
console.log(template);
Output:
Hi, Alice!
Q: Can I use single and double quotes interchangeably in strings?
A: Yes, as long as you match the opening and closing quotes correctly.
2. Number
Represents both integer and floating-point numbers.
let age = 25;
let price = 99.99;
console.log(age, price);
Output:
25 99.99
3. BigInt
Used to represent very large integers beyond the safe integer limit for numbers.
let bigNumber = 1234567890123456789012345678901234567890n;
console.log(bigNumber);
Output:
1234567890123456789012345678901234567890n
Q: Why do we use n
at the end of the number?
A: It tells JavaScript this is a BigInt
type, not a regular number.
4. Boolean
Represents logical values: true
or false
.
let isLoggedIn = true;
let hasPermission = false;
console.log(isLoggedIn, hasPermission);
Output:
true false
5. null
Represents the intentional absence of a value.
let data = null;
console.log(data);
Output:
null
6. undefined
Indicates a variable has been declared but has not yet been assigned a value.
let value;
console.log(value);
Output:
undefined
7. Symbol
Represents a unique and immutable value, often used for object keys.
let id = Symbol("userID");
console.log(id);
Output:
Symbol(userID)
Reference Data Types
1. Object
Used to store collections of data and complex entities.
let person = {
name: "Alice",
age: 30
};
console.log(person.name);
Output:
Alice
2. Array
Used to store multiple values in a single variable.
let fruits = ["apple", "banana", "cherry"];
console.log(fruits[1]);
Output:
banana
3. Function
Functions are objects that can be called (invoked).
function greet() {
return "Hello!";
}
console.log(greet());
Output:
Hello!
How to Check the Data Type
Use the typeof
operator to check the type of a variable.
let age = 25;
console.log(typeof age); // "number"
console.log(typeof null); // "object" (quirk in JavaScript)
Output:
number object
Q: Why does typeof null
return "object"
?
A: It's a long-standing bug in JavaScript that's kept for backward compatibility.
Summary Table
Type | Category | Example |
---|---|---|
String | Primitive | "hello" |
Number | Primitive | 42 |
BigInt | Primitive | 123456789n |
Boolean | Primitive | true |
null | Primitive | null |
undefined | Primitive | undefined |
Symbol | Primitive | Symbol("id") |
Object | Reference | {name: "Alice"} |
Array | Reference | [1, 2, 3] |
Function | Reference | function() {} |
Conclusion
Understanding data types is foundational in JavaScript. It helps you write better, bug-free code and understand how JavaScript manages memory and data internally.