
In JavaScript we use arrays to store a list of values. Arrays are always numbered starting from 0.
This means that the 2nd element in your array is at number 1 while the last element is always the total number minus 1.
We will first create an array, check its length and access its contents. Then we will learn the methods you can use to modify your array. We will finally see how you can check for the presence of an element withindexOf()
.
Let's say you want an array showing the countries you would like to visit...
let countries = ['morocco', 'kenya', 'malawi', 'eritrea', 'ghana', 'ethiopia'];
Now lets check the length of our array:
console.log(countries.length);
The console.log returns6
!
Now, lets say you quickly want to access the third country in your array. It will be denoted with a 2, remember what we said about the denoting the first index in our array, its always a zero.
console.log(countries[2]);
In the above case, the console.log will returnmalawi
Accessing the first country:
console.log(countries[0]);
The above code returnsmorroco
which is the first country in our array.
Let's see how arrays can easily be modified.
You have decided that Egypt needs to be on the list of countries you're visiting.
Let's use thepush()
method to add Egypt.
countries.push('egypt');
In the above case the,
console.log(countries);
will return,
[ 'morocco',
'kenya',
'malawi',
'eritrea',
'ghana',
'ethiopia',
'egypt' ]
Thepush()
method adds elementsat the end of the array while theunshift()
method adds elementsat the beginning.
Lets see theunshift()
works now.
countries.unshift('egypt');
The console.log will return:
[ 'egypt',
'morocco',
'kenya',
'malawi',
'eritrea',
'ghana',
'ethiopia' ]
Sometimes you might want to remove an element. In that case you use thepop()
andshift()
methods. How? You already guessed it right! Just aspush()
andunshift()
adds elements at the end and the beginning of the array respectively, thepop()
removes at the end whileshift()
remove elements at the beginning.
But you might want to add or remove elements at the middle or more than one element all at once. In such a case you use thesplice()
method. Other times you might want to copy or extract elements from an array as a new array object whereby you use the slice() method. In my next article I will explain splice() and slice() intensively.
Checking for the presence of an element withindexOf
. indexOf() is quite useful when say you have mutated your array so much and aren't sure if a given element exists. It returns its index if it is found and returns-1
if the element is not found.
Remember the countries? Take a look below.
let countries = ['morocco', 'kenya', 'malawi', 'eritrea', 'ghana', 'ethiopia'];
console.log(countries.indexOf('tanzania'));
The console.log will return-1
because we do not have such a country in our array.
console.log(countries.indexOf('morocco'));
The above console.log will return0
because that's the index of Morocco in our array.
Arrays come in handy when storing your data in a tabular form. For example a strict queue application or even your phone contacts among many other applications.
Thank you for reading, comment and let me know your thoughts and I'll see you in my next article. Bye.
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse