Updated April 1, 2023
Introduction to JavaScript subarray()
The JavaScript subarray() method of the PHP programming language will provide the selected array elements and the subarray() method to not change the array which is original. It is used to get a part of the original array object. It accepts only two parameters. First parameter is start1 parameter and the second one is the end1 parameter. Start1 parameter offset is inclusive whereas the end1 parameter offset is exclusive. The two parameters of the JavaScript subarray() are optional and those are mandatory at all. These two parameters of the subarray() function are the valid index values of the current array.
Syntax:
Array.subarray(start1, end1)
Explanation of the Parameters:
- Start1: The start1 parameter of the subarray() function of the PHP Programming Language is an optional parameter. This parameter is the starting position which helps us from the start of the selection.
- End1: The End1 parameter of the subarray() function of the PHP Programming Language is also an optional parameter. This End1 parameter is the ending position which helps us to know where to end selection.
- Return value of subarray(): The return value of the subarray() is the new array which is actually formed from the original array object.
How JavaScript subarray() works?
- JavaScript subarray() works based on the two parameters : start1 and end1 but these subarray() parameters are not mandatory.
- To get the sub array from the original array we have to follow one basic syntax
array.subarray(element, element)
or
array.subarray(element)
- Based on this following syntax the whole subarray() concept works.
- One can use one element inside of the subarray() predefined function/method or one can use two elements based on the requirement.
Examples of JavaScript subarray()
Given below are the examples:
Example #1
In the below example, in the head section of the <head> tag which is inside of the <html> tag <script> tag is used in order to perform the JavaScript code. Inside of the script tag a new function “profitloops1” is created with no parameters in it. Then arr1 variable is created to store the array values with the help of the Uint8Array() function. Uint8Array will represent is going to represent 8 bit unsigned integers/integer values. The contents of it are always initialized to the value “0”. Then a new variable “new_arr1” is created by specifying the values inside of the subarray() method. Now the new_arr1 stored the array values up to the index “2” of the array values. Then by using the document.write() function, original array “arr1” will be printed. Then with the help of the document.write() function only you can print the sub array element using “new_arr1” value. All this coding is done in function so after closing the function. The function is called outside of the function to execute the coding.
Syntax:
<!DOCTYPE html>
<html>
<title>Our Web Page Design1</title>
<head>
<script>
function profitloops1() {
var arr1 = new Uint8Array([12,34,56]);
var new_arr1 = arr1.subarray(2);
document.write("without using the subarray() method <br>");
document.write(arr1);
document.write("<br>");
document.write("By using the subarray()/slice() method <br>");
document.write(new_arr1);
}
profitloops1();
</script>
</head>
<body>
</body>
</html>
Output:
Example #2
In the below example, inside of the script tag an array variable “A1” is created with some numerical values in it. This is done with the help of Uint8Array() function. Then B1, C1, D1, E1 and F1 variables are created with different index values. For B1, “1, 2” index values are used which means only index values 1,2 are calling from the array which are within the 3rd index value. It contains “51, 52” values. Then “C1” variable is created to store the array values from the index value “1”. Then “D1” variable is created to store the array values from the array index value “3”. Likewise “E1” variable is created to store the array values from the index values 0 and 6. Then “F1” variable is created to store the array values starting from the index value “0”. Like this, all the different subarrays are created and stored in different variables. Then with the help of the document.write() function all the subarray elements will be printed with the help of different variables mentioned above.
Syntax:
<!DOCTYPE html>
<html>
<script>
const A1 = new Uint8Array([50, 51, 52, 53, 54, 55, 56 ]);
B1 = A1.subarray(1, 3)
C1 = A1.subarray(1)
D1 = A1.subarray(3)
E1 = A1.subarray(0, 6)
F1 = A1.subarray(0)
document.write(B1 +"<br>");
document.write(C1 +"<br>");
document.write(D1 +"<br>");
document.write(E1 +"<br>");
document.write(F1 +"<br>");
</script>
</html>
Output:
Example #3
In the below example, A1 variable is created with different numerical values inside of the array. Then different variables are created in order to get the subarray. Here now using the negative values inside of the subarray() function to know what will be the result. For “-1” index value last element in the array will be printed. For “-2” index value last two elements of the array will be printed. Likewise for the “-3” index value last 3 elements of the array will be printed. Likewise for other values too. All these sub arrays will be printed if and only if the document.write() function is used with the variables used to print the arrays.
Syntax:
<!DOCTYPE html>
<html>
<script>
const A1 = new Uint8Array([50, 51, 52, 53, 54, 55, 56 ]);
B1 = A1.subarray(-1)
C1 = A1.subarray(-2)
D1 = A1.subarray(-3)
E1 = A1.subarray(3)
F1 = A1.subarray(0)
document.write(B1 +"<br>");
document.write(C1 +"<br>");
document.write(D1 +"<br>");
document.write(E1 +"<br>");
document.write(F1 +"<br>");
&
</script>
</html>
Output:
Example #4
In the below example, a new function called profitloops1 is created. Inside of it a new array variable is created to store the values “13, 26, 39, 52, 65”. Then “new_arr11” is created to create and store the array elements from the original array. Here only the array indexes between 0 and 4 is taking into the consideration. 0 index has 13 value, 1 index has 26 value and so on. These values will be stored in new_arr11 variable. Then document.write() function is used to print the original array values and sub array elements. You can check out the output of the code so that you will know what is the difference between the original array and the sub array.
Syntax:
<!DOCTYPE html>
<html>
<script>
function profitloops1() {
var arr11 = new Uint8Array([13,26,39,52,65]);
var new_arr11 = arr11.subarray(0,4);
document.write("without using the subarray() method :: <br>");
document.write(arr11);
document.write("<br>");
document.write("By using the subarray()/slice() method :: <br>");
document.write(new_arr11);
}
profitloops1();
</script>
</html>
Output:
Recommended Articles
This is a guide to JavaScript subarray(). Here we discuss the introduction, how JavaScript subarray() works? and examples. You may also have a look at the following articles to learn more –