Predict Array map Output
Predict Output Easy
Question
What does this code print?
Code
const nums = [1, 2, 3, 4];
const doubled = nums.map(n => n * 2);
console.log(doubled);
console.log(nums);
Hint
map returns a new array and does not change the original.
Explanation
map creates a new array by applying the callback to each element: [1*2, 2*2, 3*2, 4*2] = [2, 4, 6, 8]. The original array nums is unchanged — map is non-mutating. Both arrays are printed on separate lines.
Related Resources
Related Lessons
- JavaScript Arrays
Learn about map.
Related Reference
- JavaScript Array Reference
Reference for map.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.