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

推荐订阅源

WordPress大学
WordPress大学
酷 壳 – CoolShell
酷 壳 – CoolShell
小众软件
小众软件
Vercel News
Vercel News
Last Week in AI
Last Week in AI
H
Help Net Security
The Cloudflare Blog
L
LangChain Blog
Microsoft Security Blog
Microsoft Security Blog
B
Blog RSS Feed
云风的 BLOG
云风的 BLOG
I
InfoQ
U
Unit 42
美团技术团队
人人都是产品经理
人人都是产品经理
雷峰网
雷峰网
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 叶小钗
Y
Y Combinator Blog
Hugging Face - Blog
Hugging Face - Blog
A
About on SuperTechFans
宝玉的分享
宝玉的分享
量子位
博客园_首页

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.