
JavaScript arrays are among the most often used data structures. They allow you to store and process large amounts of data efficiently. Built-in array functions simplify complex operations, making arrays even more powerful. In this article, we'll go over JavaScript array functions, including explanations and examples to help you make the most of them.
The JavaScript array functions are methods provided by the language for performing different operations such as adding, removing, searching, and manipulating array elements. They are part of the Array object prototype, hence they are available to all JavaScript arrays.
push() and pop()
The basics of array manipulation begin with adding and removing elements from the end of an array.
const fruits = ['apple', 'banana'];
fruits.push('orange'); // Returns 3 (new length)
// fruits is now ['apple', 'banana', 'orange']
const lastFruit = fruits.pop(); // Returns 'orange'
// fruits is now ['apple', 'banana']unshift() and shift()
Similar to push/pop, but applied to the beginning of the array.
// shift() example
const fruits = ['apple', 'banana', 'orange'];
const firstFruit = fruits.shift();
console.log(firstFruit); // 'apple'
console.log(fruits); // ['banana', 'orange']
console.log(fruits.length); // 2
// unshift() example
fruits.unshift('grape');
console.log(fruits); // ['grape', 'banana', 'orange']
console.log(fruits.length); // 31. Queue Implementation
class Queue {
constructor() {
this.items = [];
}
enqueue(item) {
this.items.push(item);
}
dequeue() {
return this.items.shift();
}
peek() {
return this.items[0];
}
isEmpty() {
return this.items.length === 0;
}
size() {
return this.items.length;
}
}
// Usage
const queue = new Queue();
queue.enqueue("First");
queue.enqueue("Second");
queue.enqueue("Third");
console.log(queue.dequeue()); // "First"
console.log(queue.peek()); // "Second"2. Task Processing System
class TaskProcessor {
constructor() {
this.taskQueue = [];
this.isProcessing = false;
}
addUrgentTask(task) {
this.taskQueue.unshift(task); // Add urgent task at the beginning
this.processNext();
}
addTask(task) {
this.taskQueue.push(task); // Add normal task at the end
this.processNext();
}
async processNext() {
if (this.isProcessing || this.taskQueue.length === 0) return;
this.isProcessing = true;
const task = this.taskQueue.shift();
try {
await task();
} catch (error) {
console.error('Task processing error:', error);
}
this.isProcessing = false;
this.processNext();
}
}
// Usage
const processor = new TaskProcessor();
processor.addTask(() => console.log("Normal task"));
processor.addUrgentTask(() => console.log("Urgent task")); // This will be processed first// Poor performance for large arrays
const largeArray = new Array(10000).fill(0);
// Each shift() operation requires reindexing
while (largeArray.length) {
largeArray.shift();
}
// Better alternative using pop()
while (largeArray.length) {
largeArray.pop();
}Empty Array Handling
const emptyArray = [];
const result = emptyArray.shift();
console.log(result); // undefined
console.log(emptyArray.length); // 0
// Better practice with checking
function safeShift(array) {
if (array.length === 0) {
return { success: false, value: undefined };
}
return { success: true, value: array.shift() };
}The map() function creates a new array by applying a callback function to each element of the original array.
array.map(callback(currentValue, index, array))
const numbers = [1, 2, 3, 4];
const squared = numbers.map(num => num * num);
// squared is [1, 4, 9, 16]
// Complex transformations
const users = [
{ name: 'John', age: 30 },
{ name: 'Jane', age: 25 }
];
const userNames = users.map(user => user.name);
// userNames is ['John', 'Jane']Combines map() and flat() functions, which are useful for transforming and flattening nested structures.
array.flatMap(callback(currentValue, index, array))
const contactGroups = [
{
name: 'Work',
contacts: [
{ name: 'John', email: 'john@company.com' },
{ name: 'Jane', email: 'jane@company.com' }
]
},
{
name: 'Family',
contacts: [
{ name: 'Mom', email: 'mom@gmail.com' },
{ name: 'Dad', email: 'dad@gmail.com' }
]
}
];
// Get all email addresses in a flat array
const allEmails = contactGroups.flatMap(group =>
group.contacts.map(contact => contact.email)
);
console.log(allEmails);
// ['john@company.com', 'jane@company.com', 'mom@gmail.com', 'dad@gmail.com']const schedule = [
{
date: '2025-01-01',
timeSlots: ['09:00', '10:00', '11:00']
},
{
date: '2025-01-02',
timeSlots: ['14:00', '15:00']
}
];
// Create full datetime options
const availableSlots = schedule.flatMap(day =>
day.timeSlots.map(time => ({
datetime: `${day.date} ${time}`,
formatted: `${new Date(`${day.date} ${time}`).toLocaleString()}`,
available: true
}))
);
console.log(availableSlots);
/* [
{ datetime: '2025-01-01 09:00', formatted: '1/1/2025, 9:00 AM', available: true },
{ datetime: '2025-01-01 10:00', formatted: '1/1/2025, 10:00 AM', available: true },
...
] */The filter() function returns a new array of elements that meet a specific condition.
array.map(callback(currentValue, index, array))
const numbers = [1, 2, 3, 4, 5, 6];
const evenNumbers = numbers.filter(num => num % 2 === 0);
// evenNumbers is [2, 4, 6]
// Complex filtering
const users = [
{ name: 'John', active: true },
{ name: 'Jane', active: false },
{ name: 'Bob', active: true }
];
const activeUsers = users.filter(user => user.active);const users = [
{ id: 1, name: 'John' },
{ id: 2, name: 'Jane' },
{ id: 3, name: 'Bob' }
];
const jane = users.find(user => user.name === 'Jane');
// jane is { id: 2, name: 'Jane' }
const janeIndex = users.findIndex(user => user.name === 'Jane');
// janeIndex is 1const users = [
{ id: 1, name: 'John', role: 'admin', active: true },
{ id: 2, name: 'Jane', role: 'user', active: true },
{ id: 3, name: 'Bob', role: 'user', active: false }
];
// Using find() to get user by ID
function getUserById(id) {
const user = users.find(user => user.id === id);
if (!user) {
throw new Error('User not found');
}
return user;
}
// Using findIndex() to update user status
function toggleUserStatus(id) {
const userIndex = users.findIndex(user => user.id === id);
if (userIndex === -1) {
throw new Error('User not found');
}
users[userIndex].active = !users[userIndex].active;
return users[userIndex];
}
// Example usage
console.log(getUserById(2)); // { id: 2, name: 'Jane', ... }
console.log(toggleUserStatus(3)); // { id: 3, name: 'Bob', active: true }The reduce() function applies a reducer function to each element of the array, reducing it to a single value.
array.reduce(callback(accumulator, currentValue, index, array), initialValue) const numbers = [1, 2, 3, 4]; const sum = numbers.reduce((total, num) => total + num, 0); console.log(sum); // Output: 10
// Sum of numbers
const numbers = [1, 2, 3, 4];
const sum = numbers.reduce((acc, curr) => acc + curr, 0);
// sum is 10
// Complex reduction
const orders = [
{ product: 'Widget', amount: 100 },
{ product: 'Gadget', amount: 200 },
{ product: 'Widget', amount: 300 }
];
const totalsByProduct = orders.reduce((acc, order) => {
acc[order.product] = (acc[order.product] || 0) + order.amount;
return acc;
}, {});
// totalsByProduct is { Widget: 400, Gadget: 200 }It's used to combine array items into a single value, such as summing numbers or creating an object.
Sorts elements in place, optionally using a comparison function.
// Basic sorting
const fruits = ['banana', 'apple', 'orange'];
fruits.sort();
// fruits is ['apple', 'banana', 'orange']
// Custom sorting
const numbers = [10, 2, 30, 4];
numbers.sort((a, b) => a - b);
// numbers is [2, 4, 10, 30]
// Complex object sorting
const users = [
{ name: 'John', age: 30 },
{ name: 'Jane', age: 25 },
{ name: 'Bob', age: 35 }
];
users.sort((a, b) => a.age - b.age);Powerful methods for array manipulation and extraction.
// slice(): Extract part of an array const numbers = [1, 2, 3, 4, 5]; const subset = numbers.slice(1, 4); // subset is [2, 3, 4] // splice(): Modify array in place const months = ['Jan', 'March', 'April']; months.splice(1, 0, 'Feb'); // months is ['Jan', 'Feb', 'March', 'April']
Combine arrays and create new ones.
const arr1 = [1, 2]; const arr2 = [3, 4]; // Using concat const combined1 = arr1.concat(arr2); // Using spread operator (modern approach) const combined2 = [...arr1, ...arr2]; // both combined1 and combined2 are [1, 2, 3, 4]
The forEach() function executes a provided callback function once for each array element.
array.forEach(callback(currentValue, index, array))
const numbers = [1, 2, 3, 4]; numbers.forEach(num => console.log(num)); // Output: // 1 // 2 // 3 // 4
const numbers = [1, 2, 3, 4]; const hasEven = numbers.some(num => num % 2 === 0); console.log(hasEven); // Output: true const allEven = numbers.every(num => num % 2 === 0); console.log(allEven); // Output: false
The includes() function checks if an array contains a specific value.
array.includes(value)
const fruits = ['apple', 'banana', 'cherry'];
console.log(fruits.includes('banana')); // Output: trueChoose the Right Method
Performance Optimization
// Bad: Multiple iterations
const numbers = [1, 2, 3, 4, 5];
const result = numbers
.filter(n => n % 2 === 0)
.map(n => n * 2);
// Better: Single iteration with reduce
const result = numbers.reduce((acc, n) => {
if (n % 2 === 0) {
acc.push(n * 2);
}
return acc;
}, []);Immutability
// Prefer immutable operations const original = [1, 2, 3]; const modified = [...original, 4]; // Creates new array // Instead of mutable operations original.push(4); // Modifies original array
At DevelopersMonk, we share tutorials, tips, and insights on modern programming frameworks like React, Next.js, Spring Boot, and more. Join us on our journey to simplify coding and empower developers worldwide!