Answers for ".each vs .map"

4

map vs for each

chars = ['Hello' , 'world!!!'] ;
var retVal = chars.forEach(function(word){
  console.log("Saving to db: " + word) 
})
console.log(retVal) //undefined

chars = ['Hello' , 'world!!!'] ;
var lengths = chars.map(function(word){
  return word.length
}) 
console.log(lengths) //[5,8]
Posted by: Guest on July-05-2021

Code answers related to "TypeScript"

Browse Popular Code Answers by Language