
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Explain the importance of Array.filter() method in JavaScript with example?
Array.filter()
Array.filter() method creates a new array with all the elements that have passed the test implemented by the provider function(function given by the user).In the following example the test is whether the given salary elements are more than a particular value(19000) given by the user().
Example-1
<html> <body> <p id="filter"></p> <script> var salary = [2000, 30000, 69000, 70000,78000]; function checkSal(sal) { return sal >= 19000; } document.getElementById("filter").innerHTML = salary.filter(checkSal); </script> </body> </html>
Output
30000,69000,70000,78000
Example-2
Following is the example to filter positive values from a given integer array.
<html> <body> <script> var numbers = [2,3,4,5,-9,0,-2,-5] function Positive(value) { return value > 0; } var posnum = numbers.filter(Positive); document.write(posnum); </script> </body> </html>
Output
2,3,4,5
- Related Articles
- Write the importance of shift() method in javascript array?
- Filter array with filter() and includes() in JavaScript
- Filter JavaScript array of objects with another array
- How to sort an array in JavaScript?Explain with example?
- Filter one array with another array - JavaScript
- JavaScript in filter an associative array with another array
- JavaScript example to filter an array depending on multiple checkbox conditions.
- What is meant by Splicing an array in JavaScript? Explain with an example
- What is the importance of _isEqual() method in JavaScript?
- What is the importance of _without() method in JavaScript?
- What is the importance of _.union() method in JavaScript?
- What is the importance of str.padStart() method in JavaScript?
- What is the importance of Math.trunc() method in JavaScript?
- What is the role of filter() method in JavaScript?
- Filter away object in array with null values JavaScript

Advertisements