JavaScript TypedArray subarray() Method

The subarray method in JavaScript retrieves a specified range of elements from an array without modifying the original array.

Syntax:

Example

Array.subarray(start, end)

Parameters:

Start (Optional): The initial point from which the selection process begins.

End(Optional): The concluding position that determines where the selection will cease.

Return value: Produces a fresh array that encompasses a portion of the original array.

Return value:

Browser Support:

Chrome Yes
Safari Yes
Firefox Yes
Opera Yes

Example 1

JavaScript TypedArray subarray Method.

Example

<script>

//JavaScript to illustrate subarray() method

function pyVisualizer() {

   //Original Array

    var arr = new Uint8Array([12,34,56]);

    //Extracted array

    var new_arr = arr.subarray(2);  

    document.write("without using subarray() method <br>");

    document.write(arr);

    document.write("<br>");

    document.write("using slice() method <br>");

    document.write(new_arr);

// expected output:56

}

runExample();

</script>

Output:

Example 2

JavaScript TypedArray subarray Method.

Example

<script>

//JavaScript to illustrate subarray() method

function pyVisualizer() {

//Original Array

    var arr = new Uint8Array([12,34,56,45,67]);

    //Extracted array

    var new_arr = arr.subarray(0,4);  

    document.write("without using subarray() method <br>");

    document.write(arr);

    document.write("<br>");

    document.write("using slice() method <br>");

    document.write(new_arr);

// expected output:12,34,56,45

}

runExample();

</script>

Output:

Output

12,34,56,45

Input Required

This code uses input(). Please provide values below: