Array.map in Javascript

let arr=[2,3,4];
let modArr=arr.map(function(element,index,array){
  console.log(element);
  console.log(index);
  console.log(array);
  return element;
},80);
console.log(modArr);

I want to know if it supposed to modify all my elements to 80 and print[80,80,80] in modArr???
Why is it printing [undefined,undefined,undefined].

send the link to the challenge,also map method is used to extract values in an 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 (’).

The second parameter for map is a value for this (the calling context). Passing the number 80 makes no sense. If you don’t understand why you may want to change the calling context, that isn’t going to make any sense. The second parameter isn’t “replace every value with the one there”, it’s nothing like that

I’ve never seen the second parameter used in any code, ever. How it works seems to be engine-dependent if you are getting the result of [undefined, undefined, undefined]: on Node v12-ish on my phone, passing 80 acts the same as passing undefined, which is what I would expect. So mapping the array with your function just returns a copy of the array; I can’t get it to map all the values to literal values of undefined

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