04. Array Transformation
Explanation
Arrays are ordered collections that allow you to store multiple values in a single variable, making data management efficient.
The `.map()` method is a powerful transformation tool that creates a brand new array by applying a function to every element.
It is considered a "functional" approach because it does not mutate (change) the original array, which is a best practice.
This pattern is used extensively in React and modern logic to convert raw data into UI elements or different formats.
Other critical methods include `.filter()` for removing items and `.reduce()` for calculating a single summary value.
Mastering these non-mutating array methods is essential for writing clean, predictable, and bug-free JavaScript code.
Example
arr.map(x => x * 2)
Exercise Task
Given an array of numbers [2, 4, 6, 8], use .map() to create a new array where each number is squared. Log the result.
script.js
1
2
const nums = [2, 4, 6, 8];
Console
Click "Run" to execute your code...