X



Home>Tutorial>

How to access javascript array elements or items

Admin Friday, July 10, 2020 JavaScript

In this tutorial, you will learn how to access array element or items using javascript. Every programming and scripting language deals with array and javascript is not an exception. To see more about arrays and the different methods for creating array, check this tutorial.
There are always instances where we create Array and definitely would like to access the Array items or element. The length of a JavaScript array is never fixed since it's length is subject to change anytime and same goes with the types of array elements. You can access elements of an array by using the square brackets [] and its index number. The first element of the array start with 0, the second element starts with 1, and so on till the last element. But eventually when you try to refer to array's index using negative value it will return false result. In the following example, we create an array of five strings and we are going to get value of each of this array's element. Example 1.0: Creating the Array
<script>
let cars = ['BMW', 'Camri', 'Venza','Volvo','Land Cruiser'];
</script>
And we want to access Volvo by adding "I love Volvo" for instance. This can only be possible if we have idea of the index number of Volvo else our effort we be useless. So how do you know the index number of an array's element? If you study the example 1.0 above, in layman's eye, Volvo index number will be 4 but javascript and other programming or scripting languages will tell you no the index number is 3. Yes, is 3 because they start their count from 0 and accessing this element means that we have to use index 3. So let's try it Example 1.1: Accessing Javascript Array Element using Index Number
<script>
var str="I love"
let cars = ['BMW', 'Camri', 'Venza','Volvo','Land Cruiser'];
document.write(str + cars[3]);
//Result= I love Volvo
</script>
Simple right? Now we are going to get array's element with index number -1 Example 1.2: Accessing Array with Invalid Index Number <script> var str="I love" let cars = ['BMW', 'Camri', 'Venza','Volvo','Land Cruiser']; document.write(str + cars[-1]); //Result= I love undefined </script>
So the example 1.2 above gives error which yield "I love undefined". So this means you can only get element or item from array if you use a valid index. Example 1.3: Looping Through Array's Collection If you noticed the examples above, we access all these elements using their index numbers. But there might be cases where you need to loop array's collection and get your values. Like any other programming languages, javascript has loop structure.
<script>
var str="I love"
let cars = ['BMW', 'Camri', 'Venza','Volvo','Land Cruiser'];
for(i=0;i<cars.length;i++)
{
document.write("<br>" + str + " " + cars[i]);
//Results
//I love BMW
//I love Camri
//I love Venza
//I love Volvo
//I love Land Cruiser
}
</script>

| | Views(658)

See Demo | Download File


Add your comment. Indicate code using code tag


Words: 0 | Chars: 0 | Reading Duration: 0



Images Attachment




Other Tutorials/Articles
twitter share
whatsapp share
facebook share
Emmason Integratded Services(2017-2024)
All Rights Reserved
Designed and Maintained By Emmason Integrated Services