JAVASCRIPT Tutorial
map()
function on the original array.map()
to specify how each element should be transformed.map()
does not change the original array. It only creates a new one with the transformed elements.const newArr = arr.map(callbackFunction);
const nums = [1, 2, 3, 4, 5];
const doubled = nums.map((num) => num * 2);
console.log(doubled); // Output: [2, 4, 6, 8, 10]
console.log(nums); // Output: [1, 2, 3, 4, 5] (original array remains unchanged)
// Original array of names
const names = ['John', 'Mary', 'Bob', 'Susan', 'Alice'];
// Create a new array of uppercase names
const uppercaseNames = names.map((name) => name.toUpperCase());
// Print the new array
console.log(uppercaseNames); // Output: ['JOHN', 'MARY', 'BOB', 'SUSAN', 'ALICE']
// Original array remains unaffected
console.log(names); // Output: ['John', 'Mary', 'Bob', 'Susan', 'Alice']