Thursday, October 23, 2025
HomeLanguagesJavascriptJavaScript typedArray.filter() with Example

JavaScript typedArray.filter() with Example

The Javascript typedArray.filter() is an inbuilt function in javascript that is used to form a new typedArray with the elements which satisfy the test implemented by the function provided. 

Syntax:

typedarray.filter(callback)

Parameters: It takes the parameter “callback” function which checks each element of the typedArray satisfied by the condition provided. The callback function takes three parameters that are specified below-

  • element: It is the value of the element.
  • index: It is the index of the element.
  • array: It is the array that is being traversed.

Return value: It returns a new typedarray with the elements that satisfy the test. 

JavaScript example to show the working of this function: 

Example 1: This example shows the use of the typedArray.filter() method in Javascript.

javascript




<script>
    // Calling isNegative function to check
    // elements of the typedArray
    function isNegative(element, index, array)
    {
        return element < 0;
    }
      
    // Created some typedArrays.
    const A = new Int8Array([ -10, 20, -30, 40, -50 ]);
    const B = new Int8Array([ 10, 20, -30, 40, -50 ]);
    const C = new Int8Array([ -10, 20, -30, 40, 50 ]);
    const D = new Int8Array([ -10, 20, 30, 40, -50 ]);
      
    // Calling filter() function to check condition
    // provided by its parameter
    const a = A.filter(isNegative);
    const b = B.filter(isNegative);
    const c = C.filter(isNegative);
    const d = D.filter(isNegative);
      
    // Printing the filtered typedArray
    console.log(a);
    console.log(b);
    console.log(c);
    console.log(d);
</script>


Output:

-10,-30,-50
-30,-50
-10,-30
-10,-50
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS