Array Methods
If you think about arrays like a shopping basket, these methods are simply different ways to add, remove, or transform the items inside it.
Imagine you are managing a basket of fruits. You keep adding, removing, or selecting fruits based on your needs. That is exactly how array methods work in JavaScript.
Let’s start simple.
When you use push(), you are adding an item to the end of your basket. If your basket has apples and bananas, using push will add something like mango at the end.
pop() does the opposite. It removes the last item. So if mango was the last thing you added, pop will take it out.
Now think about the front of the basket.
shift() removes the first item. If apples were at the beginning, they get removed.
unshift() adds a new item at the beginning. So if you add grapes using unshift, they will appear before everything else.
These four methods are mainly about adding and removing items.
Now let’s move to transforming data.
map() is like replacing every item in your basket with something new based on a rule.
For example, if you have numbers like 1, 2, 3 and you want to double them, map will give you 2, 4, 6. The original basket stays the same, but you get a new one with updated values.
filter() is about selecting only the items you want.
If your basket has numbers and you only want numbers greater than 10, filter will go through each item and keep only those that match the condition.
So instead of changing items, you are choosing which ones to keep.
forEach() is slightly different. It does not create a new array. It simply goes through each item and lets you perform an action.
Think of it like checking every fruit one by one. You can log it, print it, or inspect it, but you are not creating a new basket.
reduce() is where things feel a bit different, but it is very powerful.
It takes all items and combines them into a single result.
For example, if you have numbers like 1, 2, 3, 4, reduce can give you the total sum which is 10. It keeps adding each value step by step until everything is combined into one final result.
A quick comparison helps here.
Traditional loops require you to manage indexes, conditions, and results manually. Methods like map and filter make your intent clear. You directly say what you want instead of how to do it step by step.
That is why modern JavaScript prefers these methods. They are easier to read and easier to maintain.
If you are learning this, the best way is to try it yourself. Open your browser console and create a simple array of numbers. Try doubling values using map, selecting numbers using filter, and calculating sum using reduce.
Once you get comfortable with these, working with data in JavaScript becomes much more intuitive.
0
0
0