jonathanjuliani
Implementing Arrays with NodeJS and Javascript
JavaScript arrays in Node.js: essential methods, immutability patterns, and when an array beats other structures. Hands-on guide.

You use push, map, and filter on autopilot — until the array grows large or becomes the bottleneck. Then mutating vs returning a new array, or array vs another structure, actually matters. Episode 2 of the series.
What you'll learn:
- Create and access arrays in Node.js/JavaScript
- Core methods:
push,pop,shift,unshift,map,filter,reduce,forEach - Arrays of objects
- Immutability and why
mapoften beats in-place mutation - When an array is not the right structure
Prerequisites: basic JavaScript. If you have not read the series overview, start with episode 1.
JavaScript arrays: create, access, and avoid surprise mutation
Arrays, or simply lists, are the basics and considered in my opinion the foundation for developing and starting to know about data structures. They help organize, process and analyze data, so to make it easier, think of arrays as boxes that can store a variaty of items, where you can easily access, add or remove any of these items.
They are useful for dealing with collections of data in your applications. Exploring the use of arrays can really improve the quality and efficiency of your code.

The Basic: Understanding Arrays
An array is an ordered or unordered collection of elements, which can be of any type, from numbers and strings to objects and even other arrays. In NodeJS/JavaScript, and other languages, arrays are very flexible and can help you solve a lot of problems, you will use them many times.
Let's Create an Array in NodeJS/Javascript
Creating an array in Node/JavaScript is very simple, it can be started with an empty array or a already fulfilled array with element(s). Accessing and modifying its elements is equally straightforward, using indexes that start at zero.
const fruits = ['Apples', 'Bananas', 'Oranges']
const emptyArray = [] // empty array
console.log(fruits[0])
// prints: ApplesArray with objects:
const alice = {
name: 'Alice',
age: 30,
}
const bob = {
name: 'Bob',
age: 28,
}
const people = [alice, bob]
console.log(people[0])
// prints: { "name": "Alice", "age": 30 }Some Useful Array Functions
Within Node.JS/JavaScript arrays come with some built in methods/functions that facilitate operations such as adding or removing elements (push, pop, shift, unshift, among others), iterate through elements (forEach, map, filter), and much more. Let's see a summary of some here?!
push: Adds one or more elements to the end of an array and returns the new length of the array.
const arr = [1, 2, 3]
arr.push(4)
// arr now is [1, 2, 3, 4]pop: Removes the last element from an array and returns the removed element.
const arr = [1, 2, 3]
const poppedElement = arr.pop()
// arr now is [1, 2] and the pop element is 3shift: Removes the first element from an array and returns the removed element.
const arr = [1, 2, 3]
const shiftedElement = arr.shift()
// arr now is [2, 3] and the shifted element is 1unshift: Adds one or more elements to the beginning of an array and returns the new length of the array.
const arr = [2, 3]
arr.unshift(1)
// arr now is [1, 2, 3]map: Executes a callback function on each element of the array and returns a new array with the results.
Pro tip: if you don't need a new array with the results, then you don't need to use map, use forEach.
const numbers = [1, 2, 3]
const doubled = numbers.map((num) => num * 2)
// doubled now is [2, 4, 6]filter: Returns a new array with all elements that passed the test implemented by the callback function.
const numbers = [1, 2, 3, 4, 5]
const evenNumbers = numbers.filter((num) => num % 2 === 0)
// evenNumbers now is [2, 4]forEach: Executes a callback function on each element of the array, it doesn't return anything, it just does what you're asking :)
const numbers = [1, 2, 3]
numbers.forEach((num) => console.log(num))
// prints: 1 2 3What about
reduce,includes,find,findIndex,concat,slice,splice…?
There are many functions built in for arrays in NodeJS/Javascript and of course in other programming languages, if you want a simple explanation like the ones above about these functions too, let me know in the comments bellow and I'll create another post just about functions in arrays, the idea here was to give you a brief summary on what you can do with arrays.
Beyond the Basics
Managing Collections
Arrays are fundamental in for managing collections, allowing you to filter, sort and map data sets efficiently. This capability is essential for dealing with data from databases, files, or APIs.
Other Data Structures and Algorithms
Arrays are generally the base for other complex data structures such as stacks and queues. Furthermore, they are fundamental in sorting and search algorithms.
Edge cases: map does not clone nested objects
map, filter, and { ...arr } give you a new array, but elements can still be the same object references:
const users = [{ name: 'Ana', active: true }]
const copy = users.map((u) => ({ ...u }))
copy[0].active = false
console.log(users[0].active) // false — same inner objectBut I used map so I would not mutate — why did the original change? Because you copied the array, not necessarily every nested object. For deep trees use structuredClone, a library, or explicit logic — that is shallow copy, not a map bug.
Another gotcha: sort() mutates the original. Keep order intact with const sorted = [...nums].sort((a, b) => a - b).
When not to use an array: constant insert/remove in the middle of a huge list — a linked list often wins (next episode).
TL;DR — Most used array methods
| Method | What it does | Mutates? |
|---|---|---|
push(x) | Adds x at the end | Yes |
pop() | Removes and returns the last element | Yes |
shift() | Removes and returns the first element | Yes |
unshift(x) | Adds x at the start | Yes |
map(fn) | New array with fn applied | No |
filter(fn) | New array of matches | No |
reduce(fn, init) | Single accumulated value | No |
forEach(fn) | Runs fn, returns nothing | No |
find(fn) | First match | No |
includes(x) | Membership test | No |
Next steps
- Implementing Linked Lists — natural follow-up
- Implementing Stacks — LIFO on top of an array
Before you go
Which array method do you use most in production? If you spot an error in the examples, comment and we will correct it.
Subscribe to the Engineering Ledger
Get architecture and performance notes in your inbox. Same list as the timed prompt—subscribe here anytime.
Related articles

Data Structures and Algorithms - A Summary of The Most Used Ones
Practical overview of the data structures and algorithms you use most in JavaScript/Node.js — with examples and links to go deeper.
Read story
Implementing Linked-Lists with NodeJS and Javascript
Linked lists in JavaScript from scratch: nodes, insert/remove, and when they beat arrays. Practical Node.js tutorial with examples.
Read story