Loading...

JavaScript Arrays


in this tutorials we are going to learn about what is a array in javascript, how to create an array, how to access items in an array, how to change or update an item in array, how to remove an item in an array in javascript.


What is an array in javascript ?


an array is a collection of similar elements (items). it is used to store same kind of items in one variable.
it is an ordered collection of items which means it maintains its order through index.
it is used in real life scenarios like :
a car variable holds collection of car names.
a cart variable holds collection of product items.


How to create an array in javascript ?


in javascript we can create an array by following ways:
by passing values in square brackets [ ]
we assign multiple values seperated by comma ,
for example:
        		     					    		
var gadgets = ['mobile', 'psp', 'bluetooth', 'camera'];

document.write(gadgets);

output:

mobile, psp, bluetooth, camera
        		     					    		
        		     					    	
by creating an array object.
in this we pass the values in array constructor to initialize an array in javascript.
for example:
        		     					    		
var gadgets = new Array('mobile', 'psp', 'bluetooth', 'camera');

document.write(gadgets);

output:

mobile, psp, bluetooth, camera
        		     					    		
        		     					    	


Access items in an array?


in javascript we access an item value in an array by using its index.
index starts from 0 to n. here n is the length of an array.
to access an item we use variable name followed by index number which is surrounded by square brackets [ ].
for example:
        		     					    		
var shopitems = ['iphone x', 'sony camera', 'microwave oven', 'hp laptop'];

var item = shopitems[0];

document.write(item);

output:

iphone x
        		     					    		
        		     					    	


How to add or insert an item in an array in javascript ?


in javascript we add or insert an item value in an array by using push() function.
since we know array is an ordered collection of items which means it maintains its order through index.
the push() function will add the item in the end of the array.
for example:
        		     					    		
var mymovies = ['iron man 2', 'jumanji next level', 'avengers end game'];

mymovies.push('spider man');

document.write(mymovies);

output:

iron man 2, jumanji next level, avengers end game, spider man
        		     					    		
        		     					    	


How to change or update an item in an array in javascript ?


in javascript we change or update an item value in an array by using its index.
index starts from 0 to n. here n is the length of an array.
to change or update an item we use variable name followed by index number which is surrounded by square brackets [ ] and assign the new value to it.
for example:
        		     					    		
var shopitems = ['iphone x', 'sony camera', 'microwave oven', 'hp laptop'];

shopitems[0] = 'iphone 11 pro';

shopitems[1] = 'cannon dslr';

document.write(shopitems);

output:

iphone 11 pro, cannon dslr, microwave oven, hp laptop
        		     					    		
        		     					    	


How to remove or delete an item in an array in javascript ?


we can remove or delete array item by following ways:
by using pop() function.
pop() function is used when remove an larst item in array.
for example:
        		     					    		
var players = ['steve', 'rozy', 'maria', 'julie'];

players.pop();

document.write(players);

output:

steve, rozy, maria
        		     					    		
        		     					    	
by using shift() function.
shift() function is used to remove first item in an array.
for example:
        		     					    		
var players = ['steve', 'rozy', 'maria', 'julie'];

players.shift();

document.write(players);

output:

rozy, maria, julie
        		     					    		
        		     					    	
by using delete operator.
delete operator is used to remove a specific element from an array.
when an item is removed by using delete operator the value is removed
but the positioned item now holds undefined value in the array.
it is basically is used to remove item value and makes it undefined.
for example:
        		     					    		
var players = ['steve', 'rozy', 'maria', 'julie'];

delete players[1];

document.write(players);

output:

steve, , maria, julie