Skip to main content
JavaScript
filter Method (JavaScript)

Returns the elements of an array that meet the condition specified in a callback function.

array1.filter(callbackfn[, thisArg])
Parameters

Parameter

Definition

array1

Required. An array object.

callbackfn

Required. A function that accepts up to three arguments. The filter method calls the callbackfn function one time for each element in the array.

thisArg

Optional. An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.

Return Value

A new array that contains all the values for which the callback function returns true. If the callback function returns false for all elements of array1, the length of the new array is 0.

Exceptions

If the callbackfn argument is not a function object, a TypeError exception is thrown.

Remarks

The filter method calls the callbackfn function one time for each element in the array, in ascending index order. The callback function is not called for missing elements of the array.

In addition to array objects, the filter method can be used by any object that has a length property and that has numerically indexed property names.

Callback Function Syntax

The syntax of the callback function is as follows:

function callbackfn(value, index, array1)

The callback function is always called with three arguments. You can declare the callback function by using up to three parameters.

The following table lists the callback function parameters.

Callback argument

Definition

value

The value of the array element.

index

The numeric index of the array element.

array1

The array object that contains the element.

Modifying the Array Object

The filter method does not directly modify the original array, but the callback function might modify it. The following table describes the results of modifying the array object after the filter method starts.

Condition after the filter method starts

Element passed to callback function?

Element is added beyond the original length of the array.

No.

Element is added to fill in a missing element of the array.

Yes, if that index has not yet been passed to the callback function.

Element is changed.

Yes, if that element has not yet been passed to the callback function.

Element is deleted from the array.

No, unless that element has already been passed to the callback function.

Example

The following example shows how to use the filter method.

// Define a callback function.
function CheckIfPrime(value, index, ar) {
    high = Math.floor(Math.sqrt(value)) + 1;
    
    for (var div = 2; div <= high; div++) {
        if (value % div == 0) {
            return false;
        }
    } 
    return true;
}

// Create the original array.
var numbers = [31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53];

// Create an array that contains the prime numbers that are in the
// original array. The call to the filter method specifies
// the callback function as an argument.
var primes = numbers.filter(CheckIfPrime);

document.write(primes);
// Output: 31,37,41,43,47,53

In the following example, the callbackfn argument includes the code of the callback function.

// Create the original array.
var arr = [5, "element", 10, "the", true];

// Create an array that contains the string
// values that are in the original array.
var result = arr.filter(
    function (value) {
        return (typeof value === 'string');
    }
);

document.write(result);
// Output: element, the

The following example displays the names of properties that start with the letter "c" in the window.document DOM object.

The getOwnPropertyNames method provides property names to the filter method, the filter method provides the names that start with "c" to the sort method, the sort method provides sorted names to the forEach method, and the forEach method displays the names.

var obj = window.document;

Object.getOwnPropertyNames(obj).filter(CheckKey).sort().forEach(ShowKey);

// Check whether the first character of a string is "c".
function CheckKey(value) {
    var firstChar = value.substr(0, 1);
    if (firstChar.toLowerCase() == "c")
        return true;
    else
        return false;
}

function ShowKey(value) {
    document.write(value + "<br />");
}

The following example illustrates the use of the thisArg argument, which specifies an object to which the this keyword can refer.

var checkNumericRange = function(value) {
    if (typeof value !== 'number')
        return false;
    else 
        return value >= this.minimum && value <= this.maximum;
}

var numbers = [6, 12, "15", 16, "the", -12];

// The obj argument enables use of the this value
// within the callback function.
var obj = { minimum: 10, maximum: 20 }

var result = numbers.filter(checkNumericRange, obj);

document.write(result);
// Output: 12,16

The filter method can be applied to a string. The following example illustrates this.

// Define a callback function that returns true
// if the current array element follows a space
// or is the first character.
function CheckValue(value, index, ar) {
    if (index == 0)
        return true;
    else
        return ar[index - 1] === " ";
}

// Create a string.
var sentence = "The quick brown fox jumps over the lazy dog."; 

// Create an array that contains all characters that follow a space.
// The commented out statement shows an alternative syntax.
var subset = [].filter.call(sentence, CheckValue); 
// var subset = Array.prototype.filter.call(sentence, CheckValue);
 
// Convert the result array to a string.
var result = subset.join("");

document.write(result);
// Output: Tqbfjotld
Requirements

Supported in the Internet Explorer 9 standards document mode. See Version Information.

Not supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards, Internet Explorer 8 standards.