You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaScript箭头函数map方法中参数num的来源及作用疑问

Understanding JavaScript Arrow Functions with map()

Hey there! Let's unpack your questions about this map() and arrow function code—it's totally normal to scratch your head at first, so no worries.

First, let's recap your code for clarity:

let bigNumbers = [100, 200, 300, 400, 500];
let smallNumbers = bigNumbers.map(num => num / 100);

Your First Question: Where does num come from? Is it a container for new values?

Great question! The num parameter is not a container for the new values you're creating—it's a placeholder that represents each element in the original bigNumbers array as map() loops through it.

Here's what happens step by step:

  • map() starts with the first element in bigNumbers: 100. It passes this value to the arrow function, so num equals 100 for this iteration.
  • The arrow function runs num / 100 (which is 100 / 100 = 1), and this result is added to the new array that map() is building.
  • Next, map() moves to the second element: 200. Now num equals 200, the function returns 2, and that gets added to the new array.
  • This repeats for every element in bigNumbers until all elements are processed.

So num is just a variable that holds the current element from the original array during each loop iteration. The new value (like 1, 2, etc.) is what the arrow function returns, and map() collects those returns into the final smallNumbers array.

Your Second Question: Is my understanding of map() returning a new array correct?

Absolutely! That's a spot-on understanding.

map() is designed to create and return a new array where each element is the result of running your arrow function on the corresponding element in the original array. Crucially, it does not modify the original array—your bigNumbers array stays exactly as [100, 200, 300, 400, 500] after running this code. The only change is the new smallNumbers array being created with the transformed values.

Hope this clears things up! Let me know if you want to dive deeper into any part of it.

内容的提问来源于stack exchange,提问作者timSully

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:59:18