Introduction
JavaScript is a language of the Web. This series of articles will talk about my observations learned during my decade of software development experience with JavaScript.
Before moving further let us look at the previous articles of the series:
- Voice of a Developer: JavaScript Data Types - Part One
- Voice of a Developer: JavaScript Objects - Part Two
- Voice of a Developer: JavaScript Engines - Part Three
- Voice of a Developer: JavaScript Common Mistakes - Part Four
- Voice of a Developer: Editors - Part Five
- Voice of a Developer: VSCode - Part Six
- Voice of a Developer: Debugging Capabilities of VSCode - Part Seven
- Voice of a Developer: JavaScript OOP - Part Eight
- Voice of a Developer: JavaScript Useful Reserved Keywords - Part Nine
- Voice of a Developer: JavaScript Functions - Part Ten
- Voice of a Developer: JavaScript Functions Invocations - Part Eleven
- Voice of a Developer: JavaScript Anonymous Functions - Part Twelve
- Voice of a Developer: JavaScript Pure And Impure Function - Part Thirteen
- Voice of a Developer: JavaScript Closures - Part Fourteen
- Voice of a Developer: JavaScript Currying - Part Fifteen
- Voice of a Developer: JavaScript Chaining - Part Sixteen
New Array methods were introduced, and widely used now. I find these extremely useful and many modern JS libraries are heavily dependent on these, e.g., UnderscoreJS
We’ll look at commonly used Array methods
The map() method creates a new array with the results of calling a function for every array element.
Callback- produces an element of the new Array & runs over each element
Map
Parameters
- currentValue- current element being processed
- index- index of the current Array element
- array- array map was called upon
- var numbers = [1, 2, 3, 4];
- var ary1 = numbers.map(function(item, index) {
- return item * 2;
- });
- console.log(ary1); // [2, 4, 6, 8]
Therefore, the above code leverages Map method & iterates over each item and produces a new Array containing product by 2. The index is optional to pass & you could use inside callback function (item, index)
Reduce
Parameters
- previousValue- value returned previously
- currentValue- current element being processed
- index- index of the current Array element
- array- array map was called upon
Example
- var numbers = [1,2,3,4];
- var total=numbers.reduce (function (prev,current) {
- return prev+current;
- });
- console.log(total); // 10

Filter
Parameters
Callback- to test each element of the array. It returns true to keep the element, false otherwise.
- var numbers = [1,2,3,4];
- var ary1 = numbers.filter (function (value) {
- return value > 2;
- });
- console.log (ary1); // [3,4]
Find
- element- current processed value.
- index- index of current Array element.
- array- array map was called upon.
- var numbers = [1,2,3,4];
- var value = numbers.find (function (element) {
- return element == 2;
- });
- console.log(value); // 2
Every

Example
- function isPrime(number) {
- var start = 2;
- while(start <= Math.sqrt(number)) {
- if(number % start++ < 1) return false;
- }
- return number > 1;
- }
- var numbers = [3, 5, 7];
- var flag = numbers.every(isPrime);
- console.log(flag); // returns true
Rajib Das GuptaPosted Jun 7, 2016, 6:34 AM
Nice one.
Sonu ChaudharyPosted May 10, 2016, 7:34 AM
nice share
Vignesh ManiPosted May 9, 2016, 5:58 PM
Good one
Kuppurasu NagarajPosted May 9, 2016, 11:59 AM
Nice Sharing..
Debasis SahaPosted May 9, 2016, 8:37 AM
Good One..