JavaScript Array filter()

 

Definition and Usage

The filter() method creates a new array filled with elements that pass a test provided by a function.

The filter() method does not execute the function for empty elements.

The filter() method does not change the original array.


syntex

array.filter(function(currentValue, index, arr), thisValue)


thisValue=> Optional. Default undefined

A value passed to the function as its this value.

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <h1>JavaScript Array filter() Method</h1>
  <script>
    const ages = [20, 56, 23, 30, 18, 20, 70, 40, 45, 100];

    const result = ages.filter(function (curElem, index, arr) {
      return curElem < 10;
    });
    console.log(result);


  </script>
</body>

</html>

Comments

Popular posts from this blog

Javascript Array Concat and Join Method

JavaScript While Loop

JavaScript pop() Method