+ 2

What is the output? And why?

let x = [1, 2, 3] x.map(v => v * 2) alert(x[2])

28th Feb 2023, 6:14 PM
Daniel 😊😎😉[JS Challenger]
Daniel 😊😎😉[JS Challenger] - avatar
3 Answers
+ 8
3 x is unmodified. Because map creates new copy, won't affect original x. To modify, need to store back result, if you want as x = x.map( v => v*2)
28th Feb 2023, 6:20 PM
Jayakrishna 🇼🇳
+ 7
You can find out _what_ the output is by running the code. Why does it turn out like this? – map() does not work inplace, it creates a new array. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
28th Feb 2023, 6:21 PM
Lisa
Lisa - avatar
0
Daniel 😊😎😉[JS Challenger] you could force it to do an in-place modification let x = [1, 2, 3] console.log(x) x.map((_,i) => x[i]*=2) console.log(x) alert(x[2])
3rd Mar 2023, 1:28 AM
Bob_Li
Bob_Li - avatar