SA-21/06, C-11, Asapur, Varanasi, UP support@kamaldnp.com +91 8318013442
Lesson 16 of 30

JavaScript Array Methods

6 min read · Free · No login needed

An array holds a list of things — students, prices, marks, cricket scores. But a list sitting still is not very useful. You usually want to do something with every item: double every price, keep only the passing marks, print every name, find one student, or put the scores in order. JavaScript gives you five methods for exactly that: map, filter, forEach, find and sort.

Diagram — see how it fits together
Diagram: JavaScript Array Methods

Think of a chai shop. The owner has a list of prices: 10, 15, 20, 25. map is like rewriting the menu with new prices. filter is like picking only the chais under ₹20. forEach is like calling out every item on the list. find is like asking "where is the kulhad chai?" sort is like arranging the menu from cheapest to costliest. Five small tools — and almost every JavaScript program you will ever write uses at least one of them.

Remember it like this: map changes, filter keeps, forEach visits, find searches, sort orders. Say that line once and you will never mix them up again.

map — change every item

map runs a function on every item of an array and returns a new array with the results. The original array is not changed. That is the whole idea: you take a list in, you get a new list out.

let prices = [10, 15, 20, 25];
let doubled = prices.map(function(price) {
  return price * 2;
});
// doubled is [20, 30, 40, 50]
// prices is still [10, 15, 20, 25]

Read it like a sentence: "For every price, return the price times two, and collect the answers in a new array." The function you give to map receives each item one by one, and whatever it returns becomes the item in the new array.

filter — keep only what matches

filter also returns a new array, but it keeps only the items that pass a test. The test is a function that returns true or false. True means keep it, false means drop it.

let marks = [45, 82, 30, 91, 67];
let passed = marks.filter(function(mark) {
  return mark >= 40;
});
// passed is [45, 82, 91, 67]

The 30 fails the test, so it is left out. The original array is untouched. filter is the method you reach for whenever you hear "only the ones that..." — only the passed students, only the chais under ₹20, only the items in stock.

forEach — visit every item

forEach runs a function on every item, one by one — but unlike map and filter, it returns nothing. You use it when you want to do something with each item, not build a new array: print it, add it to a total, or show it on the page.

let students = ["Rahul", "Priya", "Amit"];
students.forEach(function(name) {
  console.log("Hello, " + name);
});
// Hello, Rahul
// Hello, Priya
// Hello, Amit
Note: map and forEach look similar, but they answer different questions. map asks "what is the new list?" forEach asks "what should I do with each item?" If you find yourself using map but throwing away its result, you wanted forEach.

find — search for one item

find returns the first item that passes a test. If nothing passes, it returns undefined — JavaScript's way of saying "not found".

let scores = [12, 45, 78, 33];
let firstBig = scores.find(function(score) {
  return score > 50;
});
// firstBig is 78 (the first one over 50)

Notice it stops at 78. The 33 is never checked, because find only wants the first match. If you need all the matches, use filter instead.

sort — put things in order

sort rearranges the array itself — it changes the original array, so be careful. With words, plain sort() works fine: it puts them in alphabetical order. With numbers, you must give it a small comparison function, because plain sort() treats numbers as text and would put 100 before 20.

let names = ["Priya", "Amit", "Rahul"];
names.sort();
// ["Amit", "Priya", "Rahul"]

let numbers = [100, 20, 3];
numbers.sort(function(a, b) {
  return a - b;
});
// [3, 20, 100]

The comparison function returns a negative number when a should come first, a positive number when b should come first, and zero when they are equal. a - b does exactly that for numbers, smallest first. For biggest first, use b - a.

Tip: sort is the only one of these five that changes the original array. If you want to keep the original order, copy the array first: let sorted = [...numbers].sort(function(a, b) { return a - b; }); — the three dots spread the items into a new array, and then you sort the copy.

Common mistakes

  • Forgetting that map and filter return a new array — calling them without saving the result throws the answer away.
  • Using map when you only want to print or add things up — that is forEach's job, and map wastes the new array.
  • Sorting numbers with plain sort() — it sorts them as text, so 100 comes before 20. Always pass the a - b function for numbers.
  • Expecting find to return all matches — it returns only the first one. Use filter for all of them.
  • Writing return inside a forEach and expecting it to stop the loop — forEach has no way to stop early. Use a for loop when you need to break out.

Remember

  • map changes, filter keeps, forEach visits, find searches, sort orders.
  • map, filter and find return a new value; the original array stays the same.
  • forEach returns nothing — use it for actions, not for building lists.
  • sort changes the original array, and numbers need the a - b comparison function.

Example explained

  • let marks = [45, 82, 30, 91, 67]; — the exam marks of five students, stored in one array.
  • marks.filter(function(mark) { return mark >= 40; }) — keeps only the marks that are 40 or more, so the 30 drops out and the result is a new array [45, 82, 91, 67].
  • passed.map(function(mark) { return mark + 5; }) — adds 5 grace marks to each passing mark, giving [50, 87, 96, 72]. The original marks array is never changed.
  • passed.find(function(mark) { return mark > 90; }) — finds the first mark above 90, which is 91.
  • document.getElementById("result").innerHTML = ... — writes the answer into the page, so you can see it without opening the console.

Example

Example
<!DOCTYPE html>
<html>
<head>
<title>Array Methods — Exam Marks</title>
<style>
body { font-family: Verdana, sans-serif; padding: 20px; }
h1 { color: #1e3a5f; }
button { background-color: #e13611; color: white; border: none; padding: 10px 18px; font-size: 16px; cursor: pointer; }
p { font-size: 18px; }
.result { background-color: #f1f5f9; padding: 12px; }
</style>
</head>
<body>

<h1>Exam Marks — Array Methods</h1>
<p>Marks: 45, 82, 30, 91, 67</p>
<button onclick="showResults()">Show Results</button>
<p class="result" id="result">Click the button to filter, add grace marks and find the topper.</p>

<script>
function showResults() {
  let marks = [45, 82, 30, 91, 67];
  let passed = marks.filter(function(mark) {
    return mark >= 40;
  });
  let withGrace = passed.map(function(mark) {
    return mark + 5;
  });
  let topper = passed.find(function(mark) {
    return mark > 90;
  });
  document.getElementById("result").innerHTML =
    "Passed: " + passed.join(", ") +
    "<br>With 5 grace marks: " + withGrace.join(", ") +
    "<br>First mark above 90: " + topper;
}
</script>

</body>
</html>

Output

Output — how it looks in the browser
Output of JavaScript Array Methods

Try it yourself — live editor

Change the code below and press Run to see the result instantly. No file to save, no login needed.

Result

Why learn this yourself — and not just ask AI?

AI can write code for you, but it cannot think for you. When you learn HTML yourself, your brain builds the real skill: you understand why a page looks the way it does, you can fix it when it breaks, and you can answer it in an exam or an interview without help. That is the skill that gets you a job — not a copied answer.

Get certified — just ₹10

Finish all 30 lessons, pass the test (80%), pay ₹10 and get your certificate.

Take the test

Prefer watching? Video courses teach the same topics step by step.

Go further with a full course

Free tutorials teach you the basics. Our paid courses add live projects, mentorship and a certificate.

WordPress Website Development Course (10 Days)
₹399
View
AI & ML with Azure AI, AWS AI & Generative AI
₹10,000
View
Ethical Hacking & Cyber Security Workshop
View
Browse all courses
JavaScript — Lessons
1. JavaScript Introduction 2. JavaScript Where To 3. JavaScript Output 4. JavaScript Syntax 5. JavaScript Comments 6. JavaScript Variables 7. JavaScript Operators 8. JavaScript Data Types 9. JavaScript Functions 10. JavaScript Objects 11. JavaScript Scope 12. JavaScript Events 13. JavaScript Strings 14. JavaScript Numbers 15. JavaScript Arrays 16. JavaScript Array Methods 17. JavaScript Dates 18. JavaScript Math 19. JavaScript Conditions 20. JavaScript Switch 21. JavaScript For Loops 22. JavaScript While Loops 23. JavaScript Type Conversion 24. JavaScript Errors — try, catch, finally 25. JavaScript JSON 26. JavaScript Arrow Functions 27. JavaScript Classes 28. JavaScript Promises and async/await 29. JavaScript and the DOM 30. JavaScript Form Validation
Final Test — Certificate ₹10

🤖 KAMALDNP AI Assistant

Ask about courses, website & more

Hi! I'm KAMALDNP AI Assistant. Ask me about our courses, website features, or anything else! 😊
Please wait 5s...
🛡️ 7-Day Money-Back Guarantee
🤖 KAMALDNP Assistant ✕

Request human callback:

🔐 Login Required
✕

Download karne ke liye login karo. Login ke baad yahi page pe rahoge!

Naya ho? Register karo →