A data type defines the kind of value a variable can store.
Examples:
let name = "Samir";
let age = 21;
let isStudent = true;
"Samir" → String
21 → Number
true → Boolean
Data types tell JavaScript:
Example:
let age = 21;
JavaScript knows that 21 is a Number, so mathematical operations can be performed on it.
JavaScript has two categories of data types.
| Data Type | Example |
|---|---|
| String | "Hello" |
| Number | 100 |
| Boolean | true |
| Undefined | undefined |
| Null | null |
| BigInt | 123n |
| Symbol | Symbol() |
| Object | {} |
The typeof operator returns the type of a value.
Syntax:
typeof value
Example:
console.log(typeof "Hello");
Output:
string
Strings represent textual data.
Strings can be written using:
Double quotes:
let name = "Samir";
Single quotes:
let city = 'Kathmandu';
Template literals:
let country = `Nepal`;
Example:
let message = "Hello World";
console.log(message);
Output:
Hello World
Strings can be joined together using the + operator.
let firstName = "Samir";
let lastName = "Niroula";
let fullName =
firstName + " " + lastName;
console.log(fullName);
Output:
Samir Niroula
The modern way to combine strings is with template literals.
let name = "Samir";
console.log(`Hello ${name}`);
Output:
Hello Samir
typeof "Hello";
Output:
string
Numbers represent both whole numbers and decimal numbers.
Integer:
let age = 21;
Decimal:
let price = 99.99;
let a = 10;
let b = 5;
console.log(a + b);
Output:
15
Other operations:
10 - 5;
10 * 5;
10 / 5;
10 % 3;
console.log(1 / 0);
Output:
Infinity
console.log("Hello" * 5);
Output:
NaN
NaN stands for Not a Number.
typeof 100;
Output:
number
A Boolean represents logical values.
A Boolean has only two possible values:
truefalseExample:
let isLoggedIn = true;
let isAdmin = false;
Example:
let isLoggedIn = true;
if (isLoggedIn) {
console.log("Welcome");
}
Output:
Welcome
typeof true;
Output:
boolean
A variable that has been declared but not assigned a value is undefined.
Example:
let name;
console.log(name);
Output:
undefined
Visual:
Variable Exists
↓
Value Missing
typeof undefined;
Output:
undefined
null represents an intentional absence of a value.
Example:
let user = null;
Meaning:
No User Exists Yet
Another example:
let selectedProduct = null;
Undefined:
let name;
Meaning:
Value Not Assigned
Null:
let name = null;
Meaning:
Value Intentionally Empty
typeof null;
Output:
object
This is a historical bug in JavaScript that has been kept for compatibility.
BigInt is used to store extremely large integers.
Example:
const big =
123456789012345678901234567890n;
Notice the n at the end.
Regular numbers have a maximum safe value.
Number.MAX_SAFE_INTEGER
Output:
9007199254740991
Values larger than this may lose precision.
Use BigInt instead.
Example:
const big =
999999999999999999999999999n;
console.log(big);
typeof 100n;
Output:
bigint
A Symbol creates a unique value.
Every Symbol is unique.
Example:
const id1 =
Symbol();
const id2 =
Symbol();
console.log(id1 === id2);
Output:
false
Even though both Symbols are empty.
const userId =
Symbol("id");
Common uses:
typeof Symbol();
Output:
symbol
Objects store collections of related data.
Example:
const user = {
name: "Samir",
age: 21,
isStudent: true
};
Visual:
user
├── name
├── age
└── isStudent
console.log(user.name);
Output:
Samir
Another example:
const car = {
brand: "Toyota",
model: "Corolla"
};
console.log(car.brand);
Output:
Toyota
const fruits = [
"Apple",
"Mango"
];
typeof fruits;
Output:
object
function greet() {
}
typeof greet;
Output:
function
Functions are a special type of object.
Primitive values are stored directly.
let name = "Samir";
Objects are stored by reference.
const user = {
name: "Samir"
};
let name = "Samir";
let age = 21;
let isStudent = true;
let score;
let product = null;
let big =
999999999999999n;
let id = Symbol();
const user = {
name: "Samir",
age: 21
};
typeof "Hello";
Output:
string
typeof 100;
Output:
number
typeof true;
Output:
boolean
typeof undefined;
Output:
undefined
typeof null;
Output:
object
typeof 100n;
Output:
bigint
typeof Symbol();
Output:
symbol
typeof {};
Output:
object
| Data Type | Example | typeof Result |
|---|---|---|
| String | "Hello" |
"string" |
| Number | 100 |
"number" |
| Boolean | true |
"boolean" |
| Undefined | undefined |
"undefined" |
| Null | null |
"object" |
| BigInt | 100n |
"bigint" |
| Symbol | Symbol() |
"symbol" |
| Object | {} |
"object" |
Primitive Types:
String
Number
Boolean
Undefined
Null
BigInt
Symbol
↓
SNBUNBS
Everything else is an Object.
JavaScript has 7 primitive data types and 1 major non-primitive data type.
Primitive Types:
Non-Primitive Type:
Understanding data types is important because every variable in JavaScript stores one of these types, and the available operations depend on the type of value being stored.