Skip to content
Advertisement

I can’t get the value of the variable

I’m learning JavaScript and there is a question that I can’t understand:

Consider the given code:

let names = ['ahmed', 'karima', 'hamza','soad'];  
let modifiedNames = names.forEach(name => name+99);

The value of modifiedNames here will be?

I am printing the variable in the console but it gives me undefined?!

Thank you in advance.

Advertisement

Answer

You use Array.prototype.forEach which always returns undefined. Use Array.prototype.map if you want to map your array into a new one.

let names = ['ahmed', 'karima', 'hamza','soad'];  
let modifiedNames = names.map(name => name+99);
console.log(modifiedNames);
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement