YouTip LogoYouTip

Jsref Filter

# JavaScript Array filter() Method [![Image 3: Array Object Reference Manual](#) JavaScript Array Object](#) ### Example Return all elements from the array _ages_ that are greater than 18: ```javascript var ages = [32, 33, 16, 40]; function checkAdult(age) { return age >= 18; } function myFunction() { document.getElementById("demo").innerHTML = ages.filter(checkAdult); } The output will be: 32,33,40 [Try it Yourself Β»](#) * * * ## Definition and Usage The `filter()` method creates a new array with all elements that pass the test implemented by the provided function. **Note:** `filter()` does not execute the function for array elements without values. **Note:** `filter()` does not change the original array. * * * ## Browser Support The numbers in the table specify the first browser version that fully supports the method. | Method | | | | | | | --- | --- | --- | --- | --- | --- | | filter() | Yes | 9 | 1.5 | Yes | Yes | * * * ## Syntax ```javascript array.filter(function(currentValue,index,arr), thisValue) * * * ## Parameter Values | Parameter | Description | | --- | --- | | _function(currentValue, index,arr)_ | Required. A function to run for each array element. Function parameters: | Parameter | Description | | --- | --- | | _currentValue_ | Required. The value of the current element | | _index_ | Optional. The index of the current element | | _arr_ | Optional. The array object the current element belongs to | | | _thisValue_ | Optional. A value to be passed to the function to be used as its "this" value. If thisValue is omitted, "this" is "undefined" | * * * ## Technical Details | Return Value: | An array with the elements that passed the test. If no elements pass the test, an empty array is returned. | | --- | | JavaScript Version: | 1.6 | * * * ## More Examples ### Example Return all elements from the array ages that are greater than the value specified in the input field:

Minimum Age:

All elements greater than the specified array are?

var ages = [32, 33, 12, 40]; function checkAdult(age) { return age >= document.getElementById("ageToCheck").value; } function myFunction() { document.getElementById("demo").innerHTML = ages.filter(checkAdult); } [Try it Yourself Β»](#) [![Image 4: Array Object Reference Manual](#) JavaScript Array Object](#)
← Python Five FishDocker Swarm β†’