filter() Method

By: Dusty Arlia
Published on Friday, June 7, 2013, 09:06 PM
Last Updated on Wednesday, July 08, 2015 at 10:41 PM
Total Updates: 3

The filter() method is accessed from an array object using dot-syntax. Here is the basic syntax:

[array_name].filter([callback_function][, [argument_name]])

The filter() method performs a function (callback_function) on every element in that array (array_name). It results in the creation of a new array. Only when the return value of the callback_function is true, will that value be added to the new array. callback_function() is invoked only for indexes of the array which have assigned values. The callback_function() is not invoked for indexes that have been deleted or that have never been assigned a value. Array elements which do not pass the callback_function() test are skipped, and are not included in the new array.

The callback_function creates a new array element from an element of the current array. argument_name is an object to be used as this when executing the callback_function. The second parameter, argument_name, is seldom used.