Displaying all elements of an array![]() We used one for loop to display all elements of an array by using their index. To identify how many loops we have to make we must know the total number of elements present inside the array. That we can find out like this
So this number we will set as upper limit of our number of rotation or looping. You can check our tutorial on creating array to create an array and now we will try to display each element of the array. Here is the code.
The for loop in the above code loops through the elements of the array and display one by one vertically by adding one line break at the end of each element. The upper limit of the for loop is set to script.length value which is in this case equal to 4
scripts[0] is the first element of the array and scripts[3] is the last or 4th element of the array.
Using toString()
Output is
Using Join
The output of the above code is here
Displaying single element of an arrayBy using key we can display any element of an array.
Returning Last element of the ArrayThe first element starts from 0 hence to show the last element of the array we have subtract one from total number of elements present in the array.
Returning First element of the Array
add or remove elements at any position of an array by using splice method Returning all elements with Key
Output
This article is written by plus2net.com team.
![]() |
![]() |