惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

有赞技术团队
有赞技术团队
Stack Overflow Blog
Stack Overflow Blog
爱范儿
爱范儿
B
Blog
博客园 - 三生石上(FineUI控件)
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
A
About on SuperTechFans
C
Check Point Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
Y
Y Combinator Blog
WordPress大学
WordPress大学
小众软件
小众软件
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
Recent Announcements
Recent Announcements
L
LangChain Blog
大猫的无限游戏
大猫的无限游戏
阮一峰的网络日志
阮一峰的网络日志
D
DataBreaches.Net
博客园 - 聂微东
Microsoft Security Blog
Microsoft Security Blog
美团技术团队

ByteofDev

Turning Claude into Postgres so I can raise a Series A Making Postgres 42,000x slower because I am unemployed A Quick(ish) Introduction to Tuning Postgres JavaScript numbers have an (adoption) problem My 2025 JavaScript Wishlist JavaScript Benchmarking Is a Mess Replacing Disqus Commenting A deep dive into distributed database architectures 10 ways to speed up web image loading Tailwind has a scalability problem. How can we solve that? Lerna vs Turborepo vs Rush: Which is better in 2023? The 6 JavaScript Projects to watch in 2023 Top 5 Alternatives to React in 2023 How to use ESM on the web and in Node.js React vs Svelte: Which is better in 2023? 10 ways to speed up JavaScript loading What is Bun, and does it live up to the hype? State of JS 2021 Results and Analysis State of the Web: React 10 ways to speed up web font loading State of the Web: Atomic CSS State of the Web: Static Site Generators State of the Web: Bundlers & Build Tools Migrating ByteofDev from SvelteKit to Astro State of the Web: Serverless Functions State of the Web: Deno A quick introduction to JavaScript Maps How I Built the Fastest JavaScript Data Differ State of the Web: WebAssembly When you should and shouldn't use React
Array.map() versus Array.forEach()
Jacob Jackson · 2022-01-10 · via ByteofDev

If you are learning about iterating through arrays, you have likely come across two different methods. Array.map() and Array.forEach(). At first glance, it might seem like they are the same, as they both iterate through arrays and call a function for each item. However, they are different, and many people mix them up, which can be very bad.

What Array.forEach() does

Array.forEach() is pretty simple. When you run .forEach(function) on an array, it calls function for each element with that element passed to it.

const fruits = ["bananas", "pears", "oranges"];

fruits.forEach((fruit) => {
	console.log(fruit);
});
// expected output: "bananas"
// expected output: "pears"
// expected output: "oranges"

Array.forEach() does not return anything, so if you assigned a variable to it, the variable would be set to undefined.

What Array.map() does differently

Array.map() is similar in most ways. However, it differs in one important way. Instead of not returning anything, it returns an array of new values from the result of the function callback.

let fruits = ["bananas", "pears", "oranges"];

fruits = fruits.map((fruit) => {
	return `${fruit} are awesome`;
});

console.log(fruits);
// expected output: ["bananas are awesome", "pears are awesome", "oranges are awesome"]

This can make it easy to modify every value in an array. However, it is commonly misused to do what Array.forEach() is intended to do.

Conclusion

Array.forEach() and Array.map() are related, but not the same. Use Array.map() when trying to modify every item in an array, and Array.forEach() otherwise, because you are less likely to mess up your code, and it is usually faster. Enforcing these rules also makes your code clearer and improves quality. If you found this interesting, be sure to sign up for the mailing list below to get tips and posts like these delivered to your mailbox. I hope you learned something from this, and thank you for reading.