Ad space (banner)
🟨JavaScript Lessons
Lesson 45 / 69

Pairing Up Two Arrays (a zip Operation)

In this lesson you'll learn the zip operation for pairing up two arrays, so you can handle corresponding pieces of data together. This is for people searching "JavaScript array zip" or "JavaScript combine multiple arrays."

Combining two corresponding arrays — like "an array of names" and "an array of scores" — one by one into an array of pairs is called a zip operation. Picture it like "pairing up socks, left with right." JavaScript doesn't have a dedicated zip method, but you can easily achieve it with .map().

The sample code writes names.map((name, i) => [name, scores[i]]), looping based on the array of names while combining the score at the same index to build an array of [name, score] pairs. The key trick is making good use of the index (i) passed as the second argument of .map().

A common beginner stumbling block is what happens when the two arrays have different lengths. Accessing the shorter array beyond its length returns undefined, so you may need to either confirm the lengths match ahead of time, or handle it by working to the length of the shorter array.

This technique is useful when you want to convert separately managed data into a single combined object before displaying it — an idea that's used the same way across other languages too. Restructuring several lists into a single array of objects is a common preparatory step in real work as well.

JavaScript
OUTPUT

💡 Anything passed to console.log() appears in the output below.

Ad space (banner)
Ad space (in-article)