JavaScript data types
JavaScript is a dynamically typed language, meaning variables can hold different types of data at different times during the execution of a program. Understanding the core JavaScript data types is essential for writing effective, bug-free code. In this guide, we’ll walk you through the various data types in JavaScript, from simple primitive values like numbers and strings to complex structures like objects and arrays.
By the end of this guide, you will have a clear understanding of how to use JavaScript's data types in your applications.
JavaScript has two main categories of data types:
Let’s break down each category and explore the different types in detail.
Primitive data types are the most basic types in JavaScript. They include Numbers, Strings, Booleans, Null, Undefined, Symbol, and BigInt.
A Number
represents both integers and floating-point numbers. JavaScript does not distinguish between different types of numbers (e.g., integers vs. floating-point).
Example:
let age = 30; // Integer
let price = 19.99; // Floating-point number
let hex = 0xFF; // Hexadecimal representation
NaN (Not a Number): A special numeric value representing an undefined or unrepresentable value.
let result = "hello" * 5;
console.log(result); // Output: NaN
A String
is a sequence of characters used to represent text. You can define strings using single quotes ('
), double quotes ("
), or template literals (backticks `
).
Example:
let name = "Alice";
let greeting = 'Hello, world!';
let message = `Welcome, ${name}`;
length
, toUpperCase()
, toLowerCase()
, and trim()
.A Boolean
can only have two values: true
or false
. It is commonly used in conditional statements to control the flow of logic.
Example:
let isActive = true;
let isLoggedIn = false;
if (isActive) {
console.log("The user is active.");
} else {
console.log("The user is not active.");
}
null
is a special value that represents the intentional absence of any object or value. It is explicitly assigned to variables when you want to indicate that they have no value.
Example:
let user = null;
console.log(user); // Output: null
A variable that has been declared but not assigned a value is automatically assigned the value undefined
. This can also be manually assigned.
Example:
let name;
console.log(name); // Output: undefined
A Symbol
is a unique and immutable primitive value. It is often used as a property key for object properties. Symbols are not frequently used in basic JavaScript but are useful in advanced scenarios such as metaprogramming.
Example:
let id = Symbol("id");
let user = {
[id]: "1234"
};
console.log(user[id]); // Output: 1234
A BigInt
is a special type of number that can represent integers larger than the Number
type can handle. It’s useful for working with large numbers that exceed the limitations of regular JavaScript numbers.
Example:
let bigNumber = 1234567890123456789012345678901234567890n;
console.log(bigNumber); // Output: 1234567890123456789012345678901234567890n
Reference data types are more complex data structures. These include Objects, Arrays, and Functions.
An Object
is a collection of properties and methods. Each property consists of a key (also called a "property name") and a value. Objects are often used to store structured data.
Example:
let person = {
name: "Alice",
age: 30,
isActive: true
};
console.log(person.name); // Output: Alice
console.log(person["age"]); // Output: 30
Objects can also include methods (functions that belong to an object):
let person = {
name: "Alice",
greet: function() {
console.log("Hello, " + this.name);
}
};
person.greet(); // Output: Hello, Alice
An Array
is a special type of object used for storing ordered collections of data. Arrays can hold values of any type, including other arrays or objects.
Example:
let fruits = ["Apple", "Banana", "Cherry"];
console.log(fruits[0]); // Output: Apple
fruits.push("Mango"); // Adds an element to the end
console.log(fruits); // Output: ["Apple", "Banana", "Cherry", "Mango"]
Arrays in JavaScript are zero-indexed, meaning the first element has an index of 0
.
A Function
is a block of code designed to perform a particular task. Functions are considered objects in JavaScript and can be assigned to variables, passed as arguments, or returned from other functions.
Example:
function greet(name) {
return "Hello, " + name;
}
console.log(greet("Alice")); // Output: Hello, Alice
Functions are first-class objects in JavaScript, meaning they can be assigned to variables, passed as arguments, or returned from other functions.
In JavaScript, type conversion is the process of converting a value from one data type to another. JavaScript automatically performs type conversion in certain situations, but you can also manually convert data types.
Example of Implicit Type Conversion (Type Coercion):
let number = 5;
let string = "10";
let result = number + string; // Implicit type coercion
console.log(result); // Output: "510" (number is converted to a string)
Example of Explicit Type Conversion:
let numberString = "123";
let number = Number(numberString); // Explicit conversion to a number
console.log(number); // Output: 123
let booleanValue = Boolean(1); // Converts the number 1 to a boolean (true)
console.log(booleanValue); // Output: true
To check the type of a variable, JavaScript provides the typeof
operator. It returns a string indicating the type of the operand.
Example:
let age = 30;
console.log(typeof age); // Output: "number"
let name = "Alice";
console.log(typeof name); // Output: "string"
let person = { name: "Alice", age: 30 };
console.log(typeof person); // Output: "object"
Note: typeof
returns "object"
for null
, which is a known quirk of JavaScript.