Data types are one of the most important fundamentals in JavaScript. They define the kind of value a variable can hold and how that value behaves in your program.
Whether you're storing text,
numbers, or complex data structures, understanding data types is essential for
writing correct and efficient JavaScript code.
In this guide, you’ll learn all JavaScript data types with simple explanations and practical examples.
What
Are Data Types in JavaScript?
A data type defines the type of
value a variable contains.
For example:
let
name = "John"; // string
let
age = 25; // number
let
isStudent = true; // boolean
JavaScript automatically detects the
type of data you assign to a variable. This is called dynamic typing.
Types
of Data in JavaScript
JavaScript data types are divided
into two main categories:
1.
Primitive Data Types
2.
Non-Primitive (Reference) Data Types
1. Primitive Data Types
Primitive data types store single
values and are immutable (cannot be changed directly).
1.
String
A string is used to represent text.
let
name = "JavaScript";
Strings are written using:
- Double quotes "
"
- Single quotes ' '
- Backticks ` `
2.
Number
Represents both integer and decimal
numbers.
let
age = 30;
let
price = 99.99;
JavaScript does not separate
integers and floats.
3.
Boolean
Represents logical values: true or false.
let
isLoggedIn = true;
let
isPaid = false;
Booleans are commonly used in
conditions.
4.
Undefined
A variable that has been declared
but not assigned a value.
let
value;
console.log(value);
Output:
undefined
5.
Null
Represents an intentional absence of
value.
let
data = null;
- null is
assigned manually by the developer.
- It means “empty” or “no value”.
6.
Symbol (ES6)
A unique and immutable value.
let
id = Symbol("userId");
Symbols are often used to create
unique object keys.
7.
BigInt
Used for very large integers.
let
bigNumber = 123456789012345678901234567890n;
The n at the end indicates a BigInt.
2. Non-Primitive (Reference) Data Types
Non-primitive data types can store
multiple values and are mutable.
1.
Object
Objects store data in key-value
pairs.
let
user = {
name: "John",
age: 25
};
Objects are used to represent
real-world entities.
2.
Array
Arrays store multiple values in an
ordered list.
let
fruits = ["Apple", "Banana", "Orange"];
Arrays are a special type of object.
3.
Function
Functions are reusable blocks of
code.
function
greet() {
console.log("Hello!");
}
Functions are also treated as
objects in JavaScript.
Difference
Between Primitive and Non-Primitive Types
|
Feature |
Primitive |
Non-Primitive |
|
Storage |
Single value |
Multiple values |
|
Mutability |
Immutable |
Mutable |
|
Example |
String, Number |
Object, Array |
|
Comparison |
By value |
By reference |
Type
Checking in JavaScript
You can check data types using typeof.
Example
console.log(typeof
"Hello");
console.log(typeof
10);
console.log(typeof
true);
Output:
string
number
boolean
Special
Case: typeof null
console.log(typeof
null);
Output:
object
This is a well-known JavaScript bug
that exists for historical reasons.
Type
Conversion (Bonus)
You can convert data types manually.
String
to Number
let
num = "10";
let
result = Number(num);
console.log(result);
Number
to String
let
num = 50;
let
result = String(num);
console.log(result);
Real-World
Exampl
let
user = {
name: "Alice",
age: 22,
isStudent: true
};
console.log(`${user.name}
is ${user.age} years old.`);
Common
Mistakes
1.
Confusing null and undefined
- undefined
→ not assigned
- null →
intentionally empty
2.
Forgetting typeof behavior
console.log(typeof
[]); // object
Arrays are technically objects.
Best
Practices
- Always initialize variables properly
- Use typeof for debugging
- Understand when to use objects vs arrays
- Avoid unnecessary type coercion
Conclusion
JavaScript data types are the
foundation of programming in the language. Understanding primitive and
non-primitive types helps you write cleaner, more reliable code.
Once you master data types, concepts
like variables, functions, arrays, and objects become much easier to understand
and use effectively.
Strong knowledge of data types is
the first step toward becoming a confident JavaScript developer.
