JavaScript arrays are the backbone of data processing in modern web and Node.js applications. We will go through the most important array methods, their use cases, and how to leverage them for efficient data manipulation.
At a high level, JavaScript arrays support:
const arr1 = [1, 2, 3]; const arr2 = new Array(3); // [empty × 3] const arr3 = Array.of(1, 2, 3); // [1, 2, 3]
const arr4 = Array.from('hello'); // ['h', 'e', 'l', 'l', 'o'] const arr5 = [...arr1, 4, 5]; // [1, 2, 3, 4, 5]
const zeros = Array(5).fill(0); // [0, 0, 0, 0, 0] const seq = Array.from({length: 5}, (_, i) => i + 1); // [1, 2, 3, 4, 5]
const numbers = [1, 2, 3, 4, 5]; const squares = numbers.map(n => n * n); // [1, 4, 9, 16, 25] const evens = numbers.filter(n => n % 2 === 0); // [2, 4] const sum = numbers.reduce((acc, n) => acc + n, 0); // 15 const rightSum = numbers.reduceRight((acc, n) => acc - n, 0); // (((0-5)-4)-3)-2)-1
const arr = [1, 2, 3]; arr.push(4); // [1, 2, 3, 4] arr.pop(); // [1, 2, 3] arr.unshift(0); // [0, 1, 2, 3] arr.shift(); // [1, 2, 3]
const a = [1, 2]; const b = [3, 4]; const combined = a.concat(b); // [1, 2, 3, 4] const sliced = combined.slice(1, 3); // [2, 3] const spliced = [...combined]; spliced.splice(1, 2, 9, 8); // [1, 9, 8, 4]
combined.forEach(x => console.log(x)); // logs 1, 2, 3, 4
const joined = combined.join('-'); // '1-2-3-4'
const len = combined.length; // 4
Array.isArray(combined); // true
const arr2 = ['a', 'b', 'c']; const keys = [...arr2.keys()]; // [0, 1, 2] const values = [...arr2.values()]; // ['a', 'b', 'c'] const entries = [...arr2.entries()]; // [[0, 'a'], [1, 'b'], [2, 'c']]
const arr3 = [1, 2, 3, 4, 5]; arr3.copyWithin(0, 3); // [4, 5, 3, 4, 5]
const last = arr3.at(-1); // 5
const nums = [3, 1, 4, 1, 5]; nums.sort(); // [1, 1, 3, 4, 5] (mutates original) const sorted = nums.toSorted(); // [1, 1, 3, 4, 5] (original preserved) const reversed = nums.toReversed(); // [5, 4, 3, 1, 1] (original preserved) const replaced = nums.with(2, 99); // [3, 1, 99, 1, 5] // with() replaces the value at index 2 const spliced = nums.toSpliced(1, 2, 7, 8); // [3, 7, 8, 1, 5] // toSpliced() removes 2 elements starting from index 1 and adds 7 and 8
const logs = [ {level: 'info'}, {level: 'error'}, {level: 'info'}, {level: 'error'} ]; const lastError = logs.findLast(l => l.level === 'error'); // {level: 'error'} (last one) const lastErrorIdx = logs.findLastIndex(l => l.level === 'error'); // 3
const nestedArrays = [1, [2, 3], [4, [5, 6]]]; const flat1 = nestedArrays.flat(); // [1, 2, 3, 4, [5, 6]] const flat2 = nestedArrays.flat(2); // [1, 2, 3, 4, 5, 6] const libraries = [ {name: 'Central', books: ['JS Basics', 'Node.js']}, {name: 'City', books: ['React', 'TypeScript']} ]; const allBooks = libraries.flatMap(library => library.books); // ['JS Basics', 'Node.js', 'React', 'TypeScript']
const products = [ {name: 'iPhone', category: 'Electronics'}, {name: 'Shoes', category: 'Sportswear'}, {name: 'MacBook', category: 'Electronics'} ]; const byCategory = Object.groupBy(products, p => p.category); // { Electronics: [{...}, {...}], Sportswear: [{...}] } const byCategoryMap = Map.groupBy(products, p => p.category); // Map { 'Electronics' => [{...}, {...}], 'Sportswear' => [{...}] }
For more in-depth tutorials and the latest updates, follow me:
Get instant AI-powered summaries of YouTube videos and websites. Save time while enhancing your learning experience.