The map() method is used for creating a new array by applying a provided function to each element in an existing array.
Essentially, it allows you to transform each element in an array and store the results in a new array,
while keeping the original array unchanged.
Example 1:
<script>
const numbers = [1, 2, 3, 4, 5];
const squares = numbers.map(number => number * number);
console.log(squares);
// Output: [1, 4, 9, 16, 25]
</script>
Example 2:
<script>
const people = [
{ firstName: 'John', lastName: 'Doe' },
{ firstName: 'Jane', lastName: 'Smith' },
{ firstName: 'Sam', lastName: 'Adams' }
];
const fullNames = people.map(person => `${person.firstName} ${person.lastName}`);
console.log(fullNames);
// Output: ["John Doe", "Jane Smith", "Sam Adams"]
</script>