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

推荐订阅源

Jina AI
Jina AI
Apple Machine Learning Research
Apple Machine Learning Research
宝玉的分享
宝玉的分享
M
MIT News - Artificial intelligence
S
SegmentFault 最新的问题
博客园 - 叶小钗
量子位
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - Franky
博客园 - 司徒正美
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
人人都是产品经理
人人都是产品经理
Hugging Face - Blog
Hugging Face - Blog
V
Visual Studio Blog
阮一峰的网络日志
阮一峰的网络日志
博客园 - 【当耐特】
Google DeepMind News
Google DeepMind News
L
LangChain Blog
Stack Overflow Blog
Stack Overflow Blog
博客园_首页
U
Unit 42
月光博客
月光博客
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Advanced Array Methods in JavaScript: map, filter, reduce...
Sharique Sid · 2026-05-14 · via DEV Community

Arrays are a core part of JavaScript, and as applications grow in complexity, working with arrays efficiently becomes crucial. Instead of writing manual loops, modern JavaScript gives us higher-order array methods that make our code more readable, expressive, and functional.

In this post, we’ll explore four essential array methods: map, filter, reduce, and forEach. These powerful tools will not only clean up your code but also help you think in a more declarative, functional style.

1. forEach() – Iterating Through Arrays

The forEach() method allows you to run a function on each element in an array. It’s an alternative to using a for loop.

javascript
const numbers = [1, 2, 3, 4, 5];

numbers.forEach(num => {
  console.log(num * 2);
});

Enter fullscreen mode Exit fullscreen mode

Key points:
  • Executes the provided callback once for each element.
  • Doesn’t return a new array (returns undefined).
  • Primarily useful for side effects (logging, updating UI, etc.), not transformations.

Best use case: When you want to do something with each element (e.g., logging, DOM updates).

2. map() – Transforming Arrays

The map() method creates a new array by applying a function to each element of the original.

javascript
const numbers = [1, 2, 3, 4, 5];
const squared = numbers.map(num => num * num);

console.log(squared); // [1, 4, 9, 16, 25]

Enter fullscreen mode Exit fullscreen mode

Key points:
  • Always returns a new array of the same length.
  • Doesn’t modify the original array.
  • Great for transformations like converting values, extracting properties, or restructuring data.

Best use case: When you need to transform data into a new form.

3. filter() – Selecting Subsets

The filter() method returns a new array containing only the elements that pass a given condition.

javascript
const numbers = [1, 2, 3, 4, 5];
const evens = numbers.filter(num => num % 2 === 0);

console.log(evens); // [2, 4]

Enter fullscreen mode Exit fullscreen mode

Key points:
  • Returns a new array (may be smaller than the original).
  • Doesn’t change the original array.
  • The callback must return true or false to decide whether the item stays.

Best use case: When you need to exclude or include certain elements based on conditions.

4. reduce() – Reducing to a Single Value

The reduce() method applies a function against an accumulator and each element to reduce the array to a single value.

javascript
const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce((acc, num) => acc + num, 0);

console.log(sum); // 15

Enter fullscreen mode Exit fullscreen mode

Key points:
  • Takes an accumulator (starting value, here 0) and updates it for every element.
  • Can be used for sums, products, flattening arrays, counting items, or building objects.
  • Very flexible, but can be harder to read than map or filter.
Another example—counting occurrences:
javascript
const fruits = ["apple", "banana", "apple", "orange", "banana", "apple"];

const fruitCount = fruits.reduce((acc, fruit) => {
  acc[fruit] = (acc[fruit] || 0) + 1;
  return acc;
}, {});

console.log(fruitCount); 
// { apple: 3, banana: 2, orange: 1 }

Enter fullscreen mode Exit fullscreen mode

Best use case: When you need a single result from an array (sum, average, object, etc.).

Putting It All Together

These methods can be combined for powerful transformations:

javascript
const numbers = [1, 2, 3, 4, 5, 6];

// Get the squares of even numbers and sum them up
const result = numbers
  .filter(num => num % 2 === 0)   // [2, 4, 6]
  .map(num => num * num)          // [4, 16, 36]
  .reduce((acc, num) => acc + num, 0); // 56

console.log(result); // 56

Enter fullscreen mode Exit fullscreen mode

This chain is cleaner and more expressive than multiple for loops.

Final Thoughts

Understanding forEach, map, filter, and reduce will completely change the way you work with arrays in JavaScript:

  • forEach → run a function on each element (side effects).
  • map → transform each element into something new.
  • filter → keep only the elements you want.
  • reduce → boil an array down to a single result. Once you get comfortable with these, you’ll not only write less boilerplate code but also think in a more functional, declarative style.

Stay tuned for more insights as you continue your journey into the world of web development!

Check out theYouTubePlaylist for great JavaScript content for basic to advanced topics.

Please Do Subscribe Our YouTube Channel for clearing programming concept and much more ...CodenCloud