When you write JavaScript code, you work with different kinds of values. These values can be text, numbers, true/false, or even more complex things like arrays or key-value pairs. In JavaScript, these values are classified into data types.
JavaScript has two main types of data:
- Primitive Data Types
- Non-Primitive Data Types
Let’s understand each one with examples.
Primitive Data Types
These data types are simple values and are stored directly in memory and they are immutable.
JavaScript provides the following primitive data types:
string
number
boolean
undefined
null
symbol
bigint
String
This is used to store any kind of text.
For example:
let name = "Shefali";
let greeting = "Hello!";
let message = `Today is a great day.`;
For writing the strings, you can use:
-
"Double quotes"
-
'Single quotes'
-
Backticks
, (used for dynamic text and these are called template literals)For example:
let age = 26; let message = `I am ${age} years old.`; console.log(message); // Output: I am 26 years old.
Note: Strings are immutable, which means you can't change their value.
let name = "Milan";
name[0] = "K";
console.log(name); // This will print "Milan", not "Kilan"
Number
This is used to store integers, decimals, and negative values.
For example:
let count = 10;
let price = 99.99;
let temperature = -5;
Fun fact:
typeof NaN; // "number"
NaN (Not a Number) is still... a number.
Boolean
This is used to store true or false values, for conditions and decisions.
For example:
let isLoggedIn = true;
let hasPremium = false;
// You can use these boolean values for conditions like this:
if (isLoggedIn) {
console.log("Welcome back!");
}
Undefined
A variable that's declared but not assigned a value.
For example:
let score;
console.log(score); // undefined
undefined
also occurs when a function returns nothing.
For example:
function doNothing() {}
console.log(doNothing()); // undefined
Null
This is used when you provide no value to a variable intentionally. Like you’re saying, "I know this should exist, but not yet."
For example:
let selectedUser = null;
Fun fact:
typeof null
returns"object"
and it’s a long-standing bug in JavaScript and still exists because fixing it would break too much old code.
Symbol
You can use this when you need a value that’s guaranteed to be unique.
For example:
let id1 = Symbol("id");
let id2 = Symbol("id");
console.log(id1 === id2); // false
BigInt
This can be used for very large numbers.
For example:
let big = 1234567890123456789012345678901234567890n;
Note: You can’t mix bigint
with normal numbers.
For example:
let x = 10n + 20n; // Valid
let y = 10 + 20n; // Uncaught TypeError: Cannot mix BigInt and other types
Non-Primitive (Reference) Data Types
These types are used to hold collections of values or more complex structures.
JavaScript provides object
data type as non-primitive data type that includes objects, arrays, dates, maps, sets, promises, and more.
Let’s understand these with examples.
Object
Objects are key-value pairs.
For example:
let user = {
name: "Shefali",
age: 26,
isDev: true,
};
Here, user
is an object in which name
is a key and Shefali
is a value, age
is a key and 26
is a value, isDev
is a key and true
is value.
In objects,
- Keys are called properties
- Values can be any data type and can also contain functions (called methods)
Array
Arrays are ordered lists and technically a type of object.
For example:
let fruits = ["apple", "banana", "mango"];
console.log(fruits[1]); // "banana"
Function
Functions are used for reusable code and these are a special type of object in JavaScript.
For example:
function greet(name) {
return `Hello, ${name}`;
}
typeof Operator
In JavaScript, you can use the typeof
operator to check the data type of any value.
For example:
typeof "hello"; // "string"
typeof 42; // "number"
typeof true; // "boolean"
typeof undefined; // "undefined"
typeof null; // "object"
typeof Symbol(); // "symbol"
typeof 123n; // "bigint"
typeof [1, 2, 3]; // "object" (array)
typeof { a: 1 }; // "object"
typeof function () {}; // "function"
Summary of Data Types
Type | Example | typeof |
---|---|---|
string | "hello" |
"string" |
number | 10 , 99.99 |
"number" |
boolean | true , false |
"boolean" |
undefined | let x; |
"undefined" |
null | null |
"object" |
symbol | Symbol("id") |
"symbol" |
bigint | 12345678901234567n |
"bigint" |
object | { name: "Shefali", age: 26, isDev: true } |
"object" |
array | ["apple", "banana", "mango"] |
"object" |
function | function greet() { return "Hello!"; } |
"function" |