JavaScript Data Types
JavaScript data types describe the kind of value that a variable can store. Every value in JavaScript has a type, such as a number, text, Boolean value, object, or an empty value.
Understanding data types is one of the most important steps in learning JavaScript. Data types affect how values are stored, compared, calculated, displayed, and passed to functions.
JavaScript is a dynamically typed language. This means you do not have to declare the data type of a variable when creating it. The type is determined automatically from the value assigned to the variable.
For example:
let name = "Dibya";
let age = 25;
let isStudent = true;
Here:
namecontains a string.agecontains a number.isStudentcontains a Boolean value.
JavaScript can also change the type of a variable during program execution:
let value = 100;
value = "Hello";
The variable value first contains a number and later contains a string.
What Are Data Types in JavaScript?
A data type tells JavaScript what kind of value it is working with.
For example:
let city = "Bhubaneswar";
let temperature = 32;
let raining = false;
The values have different types:
"Bhubaneswar" → String
32 → Number
false → Boolean
JavaScript has eight standard data types:
- String
- Number
- BigInt
- Boolean
- Undefined
- Null
- Symbol
- Object
The first seven are primitive data types. Object is the non-primitive category.
Primitive and Non-Primitive Data Types
JavaScript data types can broadly be divided into two groups:
Primitive Data Types
Primitive values represent a single value and are immutable.
The primitive types are:
- String
- Number
- BigInt
- Boolean
- Undefined
- Null
- Symbol
For example:
let name = "Alex";
let age = 30;
let active = true;
Non-Primitive Data Type
The main non-primitive data type in JavaScript is:
- Object
Objects can contain multiple values and can represent more complex structures.
let person = {
name: "Alex",
age: 30,
city: "Bhubaneswar"
};
Arrays and functions are also technically objects in JavaScript.
1. String Data Type
A string represents text.
Strings can be written using:
- Double quotes
" " - Single quotes
' ' - Backticks
` `
Examples:
let firstName = "Dibya";
let lastName = 'Mendali';
let message = `Hello World`;
All three values are strings.
Double Quotes
let language = "JavaScript";
Single Quotes
let language = 'JavaScript';
Template Literals
Backticks create template literals.
let name = "Dibya";
let message = `Hello, ${name}!`;
console.log(message);
Output:
Hello, Dibya!
Template literals are especially useful when you need to combine text with variables.
Strings Can Contain Numbers
A value that looks like a number is still a string when it is enclosed in quotes.
let age = "25";
This is a string, not a number.
Compare:
let a = 25;
let b = "25";
Here, a is a number and b is a string.
You can check the type using typeof:
console.log(typeof a);
console.log(typeof b);
Output:
number
string
2. Number Data Type
The Number type is used for numeric values.
It can represent:
- Integers
- Decimal numbers
- Positive numbers
- Negative numbers
- Special numeric values
Examples:
let age = 25;
let price = 99.99;
let temperature = -5;
All of these are numbers.
Arithmetic With Numbers
let a = 10;
let b = 5;
console.log(a + b);
console.log(a - b);
console.log(a * b);
console.log(a / b);
Output:
15
5
50
2
JavaScript Number Precision
JavaScript numbers generally use IEEE 754 double-precision floating-point representation.
This can produce surprising results with some decimal calculations:
console.log(0.1 + 0.2);
The result may be:
0.30000000000000004
This happens because many decimal fractions cannot be represented exactly in binary floating-point format.
For financial calculations, applications often need special techniques or decimal libraries rather than relying blindly on floating-point arithmetic.
Special Number Values
JavaScript’s Number type includes some special values.
Infinity
let result = 10 / 0;
console.log(result);
Output:
Infinity
Negative infinity is also possible:
console.log(-10 / 0);
Output:
-Infinity
NaN
NaN means Not-a-Number.
It represents an invalid or unsuccessful numeric result.
let result = "Hello" / 2;
console.log(result);
Output:
NaN
Interestingly, NaN has the type number:
console.log(typeof NaN);
Output:
number
The name can be confusing, but this behavior is part of JavaScript’s numeric model.
3. BigInt Data Type
BigInt is used for integers larger than the safe integer range of the JavaScript Number type.
A BigInt can be created by adding n to an integer:
let bigNumber = 123456789012345678901234567890n;
You can also use the BigInt() function:
let number = BigInt("123456789012345678901234567890");
Why BigInt Is Useful
JavaScript Number can safely represent integers only up to:
Number.MAX_SAFE_INTEGER
This value is:
9007199254740991
For integers larger than this, BigInt can provide exact integer arithmetic.
Example:
let a = 9007199254740993n;
let b = 2n;
console.log(a + b);
BigInt and Number Cannot Usually Be Mixed
This causes an error:
let a = 10n;
let b = 5;
console.log(a + b);
Instead, use the same numeric type:
let a = 10n;
let b = 5n;
console.log(a + b);
Output:
15n
BigInt is intended for integer arithmetic. It does not represent fractional values.
4. Boolean Data Type
The Boolean data type has only two possible values:
true
and
false
Booleans are commonly used for conditions and decision-making.
Example:
let isLoggedIn = true;
let isAdmin = false;
A simple condition can use a Boolean:
let age = 20;
if (age >= 18) {
console.log("Adult");
}
Comparison expressions usually produce Boolean values:
console.log(10 > 5);
Output:
true
Another example:
console.log(10 === 20);
Output:
false
5. Undefined Data Type
undefined means a value has not been assigned.
For example:
let name;
console.log(name);
Output:
undefined
The variable exists, but it currently has no assigned value.
You can check its type:
console.log(typeof name);
Output:
undefined
A function that does not explicitly return a value also returns undefined:
function test() {
}
console.log(test());
Output:
undefined
Undefined can also appear when accessing a missing object property:
let person = {
name: "Alex"
};
console.log(person.age);
Output:
undefined
6. Null Data Type
null represents an intentional absence of a value.
For example:
let selectedUser = null;
This can communicate that the variable is deliberately empty or currently has no object value.
Later, it can be assigned an actual object:
selectedUser = {
name: "Alex"
};
Null vs Undefined
Although null and undefined both indicate an absence of a useful value, they have different meanings.
undefined often means a value has not been assigned or is unavailable.
null is usually assigned intentionally to indicate an empty or missing value.
Example:
let a;
let b = null;
Here:
aisundefined.bisnull.
The Famous typeof null Behavior
There is a historical JavaScript quirk:
console.log(typeof null);
The result is:
object
This is widely considered a legacy behavior of JavaScript.
It does not mean that null is actually an object. null is its own primitive value.
When you need to specifically test for null, use:
value === null
7. Symbol Data Type
Symbol is a primitive data type used to create unique identifiers.
A Symbol can be created using:
let id = Symbol();
Every newly created Symbol is unique:
let a = Symbol();
let b = Symbol();
console.log(a === b);
Output:
false
Even if two Symbols have the same description, they are still different:
let a = Symbol("id");
let b = Symbol("id");
console.log(a === b);
Output:
false
Symbols as Object Property Keys
Symbols can be used as unique property keys:
const id = Symbol("id");
const user = {
name: "Alex",
[id]: 123
};
console.log(user[id]);
Symbols are useful when you want a property key that is unlikely to conflict with ordinary string-based property names.
8. Object Data Type
An object is a collection of related data and functionality.
A basic object looks like this:
let person = {
name: "Dibya",
age: 25,
city: "Bhubaneswar"
};
The object contains properties:
name
age
city
You can access a property using dot notation:
console.log(person.name);
Output:
Dibya
You can also use bracket notation:
console.log(person["age"]);
Output:
25
Objects Can Contain Different Data Types
An object can store different types of values:
let product = {
name: "Laptop",
price: 50000,
available: true,
discount: null
};
Objects can also contain arrays, functions, and other objects.
Arrays Are Objects
Arrays are commonly described separately because they are used to store lists of values.
However, technically, an array is an object in JavaScript.
Example:
let fruits = ["Apple", "Banana", "Mango"];
You can check:
console.log(typeof fruits);
Output:
object
To specifically check whether a value is an array, use:
Array.isArray(fruits);
This returns:
true
Arrays Can Store Different Types
JavaScript arrays can contain values of different types:
let data = [
"JavaScript",
100,
true,
null
];
Although this is allowed, keeping arrays logically consistent usually makes code easier to understand.
Functions Are Objects
Functions have special behavior because they can be called, but they are also objects in JavaScript’s type system.
For example:
function greet() {
console.log("Hello");
}
You can check:
console.log(typeof greet);
Output:
function
function is a special result of the typeof operator, while functions are objects from the perspective of JavaScript’s object model.
Functions can also have properties:
function greet() {
console.log("Hello");
}
greet.language = "JavaScript";
console.log(greet.language);
The typeof Operator
The typeof operator is one of the easiest ways to determine the type of a value.
Examples:
console.log(typeof "Hello");
console.log(typeof 100);
console.log(typeof true);
console.log(typeof undefined);
console.log(typeof 100n);
console.log(typeof Symbol());
console.log(typeof {});
Typical output:
string
number
boolean
undefined
bigint
symbol
object
There are two important special cases to remember:
typeof null
returns:
object
And:
typeof function() {}
returns:
function
JavaScript Data Types at a Glance
| Data Type | Example | Description |
|---|---|---|
| String | "Hello" | Text |
| Number | 42 | Numeric values |
| BigInt | 42n | Large integers |
| Boolean | true | Logical true or false |
| Undefined | undefined | Value not assigned |
| Null | null | Intentional absence of value |
| Symbol | Symbol("id") | Unique identifier |
| Object | {name: "Alex"} | Collection of properties |
Dynamic Typing in JavaScript
JavaScript uses dynamic typing.
You do not have to specify the type when declaring a variable:
let value = 100;
JavaScript determines that value contains a number.
The same variable can later contain another type:
value = "Hello";
Now it contains a string.
It can change again:
value = true;
Now it contains a Boolean.
This flexibility is convenient, but it also means developers need to pay attention to the values flowing through their programs.
Static Typing vs Dynamic Typing
In a statically typed language, a variable’s type is generally checked more strictly.
For example, a language may require you to declare:
integer age
JavaScript does not require this:
let age = 25;
The type is determined at runtime.
JavaScript’s dynamic typing makes development flexible, but it can also allow unexpected type-related behavior if values are not handled carefully.
Type Conversion in JavaScript
Type conversion means changing a value from one type to another.
JavaScript supports both explicit and implicit conversion.
Converting String to Number
You can use Number():
let value = "100";
let number = Number(value);
console.log(number);
Output:
100
You can also use:
parseInt("100", 10);
or:
parseFloat("10.5");
Use Number() when you want general numeric conversion and parseInt() or parseFloat() when parsing numeric text according to their specific behavior.
Converting Number to String
Use String():
let number = 100;
let text = String(number);
console.log(typeof text);
Output:
string
You can also use:
let text = number.toString();
Converting to Boolean
Use Boolean():
console.log(Boolean(1));
console.log(Boolean(0));
Output:
true
false
Truthy and Falsy Values
JavaScript uses the idea of truthy and falsy values when values are evaluated in Boolean contexts.
Common falsy values include:
false
0
-0
0n
""
null
undefined
NaN
Most other values are truthy.
For example:
if ("Hello") {
console.log("This runs");
}
An empty string is falsy:
if ("") {
console.log("This will not run");
}
One important point is that an empty array and empty object are truthy:
Boolean([])
returns:
true
and:
Boolean({})
also returns:
true
Type Coercion
JavaScript sometimes automatically converts one type into another during an operation. This is called type coercion.
For example:
console.log("10" + 5);
The result is:
105
The number 5 is converted to a string because the + operator is being used for string concatenation.
Another example:
console.log("10" - 5);
The result is:
5
Here JavaScript converts the string "10" into a number.
Because these rules can sometimes be surprising, explicit conversion often makes code clearer.
Equality and Data Types
JavaScript provides two common equality operators:
==— loose equality===— strict equality
Loose Equality
Loose equality can perform type conversion:
console.log(5 == "5");
Output:
true
Strict Equality
Strict equality checks both value and type:
console.log(5 === "5");
Output:
false
In most application code, === and !== are preferred because they avoid many implicit-conversion surprises.
Primitive Values Are Immutable
Primitive values themselves cannot be changed.
For example:
let word = "Hello";
You cannot modify the existing string character directly:
word[0] = "Y";
The string remains unchanged.
Instead, you create a new string:
word = "Yellow";
The variable now refers to a new string value.
This concept is different from whether a variable declared with let or const can be reassigned.
Objects Are Mutable
Objects can generally be modified after creation.
For example:
let person = {
name: "Alex"
};
person.name = "John";
console.log(person.name);
Output:
John
The object’s property was changed.
This is also important when using const.
const Does Not Make an Object Immutable
Consider:
const person = {
name: "Alex"
};
You cannot reassign the variable:
person = {};
That causes an error.
But you can change a property:
person.name = "John";
This works.
Therefore, const prevents reassignment of the variable binding. It does not automatically freeze the object.
If you need an object to be protected from ordinary modifications, you can consider:
Object.freeze(person);
However, Object.freeze() is shallow and does not recursively freeze nested objects.
Reference Values
Primitive values are handled differently from objects.
Consider:
let a = 10;
let b = a;
b = 20;
console.log(a);
Output:
10
Changing b does not change a.
With objects:
let person1 = {
name: "Alex"
};
let person2 = person1;
person2.name = "John";
console.log(person1.name);
Output:
John
Both variables refer to the same object.
This is why understanding references is important when working with arrays and objects.
Comparing Objects
Two separate objects are not equal simply because they contain the same data.
let a = {
name: "Alex"
};
let b = {
name: "Alex"
};
console.log(a === b);
Output:
false
They are two different object references.
However:
let a = {
name: "Alex"
};
let b = a;
console.log(a === b);
Output:
true
Both variables refer to the same object.
null, undefined, and Empty Strings
These values are often confused.
Undefined
let value;
The value is undefined.
Null
let value = null;
The value is intentionally empty.
Empty String
let value = "";
The value is a string containing zero characters.
These are different values:
undefined
null
""
Their meanings depend on the situation.
Checking for Null or Undefined
A strict check is usually the clearest approach:
if (value === null) {
console.log("Value is null");
}
For undefined:
if (value === undefined) {
console.log("Value is undefined");
}
You can also use:
typeof value === "undefined"
when appropriate.
Modern JavaScript also provides the nullish coalescing operator:
let name = userName ?? "Guest";
The fallback is used when userName is null or undefined.
The Nullish Coalescing Operator
The ?? operator is useful when you want a default value only for null or undefined.
Example:
let username = null;
let displayName = username ?? "Guest";
console.log(displayName);
Output:
Guest
This is different from ||.
For example:
let count = 0;
console.log(count || 10);
This produces:
10
But:
console.log(count ?? 10);
produces:
0
This distinction is important when 0, false, or an empty string is a valid value.
Optional Chaining and Data Types
Optional chaining ?. can safely access properties when an object might be null or undefined.
Example:
let user = null;
console.log(user?.name);
Instead of throwing an error, the expression evaluates to:
undefined
Optional chaining is particularly useful when working with nested data from APIs.
console.log(user?.address?.city);
typeof Is Not Always Enough
Although typeof is useful, it cannot distinguish all JavaScript types.
For example:
typeof []
returns:
object
And:
typeof null
also returns:
object
For arrays, use:
Array.isArray(value);
For more advanced object inspection, you can use techniques such as:
Object.prototype.toString.call(value);
or inspect the object’s constructor carefully when appropriate.
Dates Are Objects
A JavaScript Date is an object:
let today = new Date();
console.log(typeof today);
Output:
object
You can check whether a value is a Date using:
value instanceof Date
For example:
let today = new Date();
console.log(today instanceof Date);
Output:
true
Regular Expressions Are Objects
Regular expressions are also objects:
let pattern = /javascript/i;
console.log(typeof pattern);
Output:
object
Regular expressions are used to search, match, and manipulate text.
Maps and Sets Are Objects
Modern JavaScript also provides collections such as Map and Set.
Example:
let users = new Map();
users.set("id", 101);
A Set stores unique values:
let numbers = new Set([1, 2, 2, 3]);
console.log(numbers);
The duplicate value is removed.
Both Map and Set are objects.
Data Types and Function Parameters
JavaScript does not require you to specify parameter types.
For example:
function add(a, b) {
return a + b;
}
You can call it with numbers:
add(10, 20);
But different types can produce different behavior:
add("10", 20);
This returns:
1020
This is another reason why developers should understand JavaScript’s type coercion rules.
Data Types and APIs
When working with APIs, data types become especially important.
For example, an API might return:
{
"name": "Alex",
"age": 25,
"active": true
}
Here:
"Alex"is a string.25is a number.trueis a Boolean.
But data received from an external source should not automatically be assumed to have the expected type. Validation may be necessary before using it.
Common JavaScript Data Type Mistakes
Mistake 1: Confusing a Number With a String
let age = "25";
This is a string.
If you need a number:
let age = Number("25");
Mistake 2: Using typeof to Check Arrays
This:
typeof []
returns:
object
Use:
Array.isArray([]);
instead.
Mistake 3: Assuming null Has Type Null
This:
typeof null
returns:
object
This is a historical JavaScript behavior.
Mistake 4: Mixing BigInt and Number
Avoid:
10n + 5
Use:
10n + 5n
when BigInt arithmetic is intended.
Mistake 5: Relying Too Much on Implicit Conversion
Code such as:
"10" + 5
may produce results that are not what beginners expect.
Explicit conversion is often easier to understand:
Number("10") + 5
Mistake 6: Thinking const Makes Objects Immutable
This:
const user = {
name: "Alex"
};
does not prevent:
user.name = "John";
The variable cannot be reassigned, but the object’s contents can still be changed unless the object is otherwise protected.
Best Practices for Working With Data Types
Use Meaningful Variable Names
Prefer:
let userAge = 25;
instead of:
let x = 25;
Meaningful names make the expected data easier to understand.
Prefer Strict Equality
Use:
value === expected
instead of relying on loose equality unless you intentionally want its coercion behavior.
Convert Data Explicitly
Instead of depending on automatic conversion:
let total = price + Number(tax);
Explicit conversion makes your intention clear.
Validate External Data
When receiving values from forms, APIs, URLs, or other external sources, check their types before using them.
Use const by Default When Appropriate
If a variable does not need reassignment, const communicates that intention.
Use let when reassignment is actually needed.
Use BigInt for Very Large Integers
When exact integer values exceed the safe range of Number, consider BigInt.
Do Not Overuse Different Types
Although JavaScript allows a variable to change types, keeping a variable conceptually consistent often improves readability and reduces bugs.
JavaScript Data Types Example
The following example demonstrates several JavaScript data types together:
const name = "Dibya";
let age = 25;
const isStudent = true;
const score = 95.5;
const largeNumber = 12345678901234567890n;
const emptyValue = null;
let notAssigned;
const user = {
name: name,
age: age,
active: isStudent
};
console.log(typeof name);
console.log(typeof age);
console.log(typeof isStudent);
console.log(typeof score);
console.log(typeof largeNumber);
console.log(typeof emptyValue);
console.log(typeof notAssigned);
console.log(typeof user);
This example demonstrates strings, numbers, Boolean values, BigInt, null, undefined, and objects.
Quick JavaScript Data Type Test
You can use the following example to practice:
let a = "Hello";
let b = 100;
let c = true;
let d;
let e = null;
let f = 100n;
let g = Symbol("id");
let h = {};
console.log(typeof a);
console.log(typeof b);
console.log(typeof c);
console.log(typeof d);
console.log(typeof e);
console.log(typeof f);
console.log(typeof g);
console.log(typeof h);
Expected results:
string
number
boolean
undefined
object
bigint
symbol
object
Remember that the object result for null is a historical quirk.
Frequently Asked Questions About JavaScript Data Types
How many data types are there in JavaScript?
JavaScript has eight standard data types: String, Number, BigInt, Boolean, Undefined, Null, Symbol, and Object.
What is the most commonly used JavaScript data type?
Strings, numbers, Booleans, objects, and arrays are among the most frequently used types in everyday JavaScript development.
Is JavaScript statically typed?
No. JavaScript is dynamically typed. The type of a value is determined at runtime.
Is an array a data type in JavaScript?
Arrays are commonly treated as a separate category in tutorials, but technically arrays are objects in JavaScript.
What is the difference between null and undefined?
undefined commonly means a value has not been assigned or is unavailable. null usually represents an intentional absence of a value.
What does typeof do?
The typeof operator returns a string describing the type of a value.
Example:
typeof "Hello"
returns:
string
Why does typeof null return object?
It is a long-standing historical behavior in JavaScript. It should not be interpreted as meaning that null is actually an object.
What is BigInt used for?
BigInt is used for integers that are too large to be represented safely by JavaScript’s Number type.
What is a primitive data type?
A primitive is a basic JavaScript value. The primitive types are String, Number, BigInt, Boolean, Undefined, Null, and Symbol.
Are functions objects in JavaScript?
Functions are callable objects. The typeof operator reports "function" for functions as a special case.
What is type coercion?
Type coercion is the conversion of a value from one type to another. JavaScript can perform this conversion automatically in certain operations.
Which equality operator should beginners generally use?
=== is generally preferred because it compares values without performing the type conversion associated with loose equality.
Final Thoughts
JavaScript data types are the foundation of JavaScript programming. Every variable, function argument, object property, API response, and calculation involves values with specific types.
The eight standard data types are:
String
Number
BigInt
Boolean
Undefined
Null
Symbol
Object
The most important concepts to remember are that JavaScript is dynamically typed, primitive values differ from objects, arrays and functions are objects in the language’s object model, and JavaScript can automatically convert values between types in some situations.
A strong understanding of data types makes it much easier to understand variables, operators, conditions, functions, arrays, objects, APIs, and modern JavaScript features.
When writing real-world JavaScript, prefer clear types, explicit conversions, strict comparisons, meaningful variable names, and appropriate validation. These simple habits can make your code easier to read, maintain, debug, and scale.