Map operator js

let cars = ['Mersedes', 'Reno', 'BMW', 'Porsche']
let up = cars.map(car =>{
      return car.toUpperCase()
})
console.log(up)

can someone explain, how is parametr ‘car’ is connected to array ‘cars’? how this return can effect the array?

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Hey @alastrenok ,

The .map takes your callback and passes in the values inside the cars. This is what a .map function would look like:

Array.prototype.map = function(callback) {
  for (let i = 0; i < this.length; i++) {
    callback(this[i])// 'this' is the array.
  }
}

So essentially, the map function takes your function and run it by giving it the variable.

Here’s a more detailed and easier to understand explanation from an MDN article:
https://developer.mozilla.org/en-us/docs/Web/JavaScript/Reference/Global_Objects/Array/map

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.