Hello Folks, In this article we are going to learn about Arrays using JavaScript.
What is an Array?
Array is a Data Structure where you can store values in a singe variable name and can access the values by referring to an index number.
var names = ['guru','hemanth','sushan'];
Accessing Array Element
You can Access any element in an Array by its index number.
var names = ['guru','hemanth','sushan'];
/ In array index number starts from 0. So the index value of guru is 0, hemanth is 1, and sushan is2/
console.log(names[1]);
/This will return an output hemanth/
Changing an Element
You change an element in an array by using its index number.
var names = ['guru','hemanth','sushan'];
names[0] = 'gaurav';
/The updated array will be/
names = ['gaurav','hemanth','sushan']
Length Property
By Using the
Length
property in an Array we can determine the length of the Array.
var names = ['guru','hemanth','sushan'];
console.log(names.length);
/The output will be 3 /
Adding an Element
By using an
push
keyword in an array we can add an element into an array. The push method adds an element to the end of the array.
var names = ['guru','hemanth','sushan'];
names.push('gaurav');
console.log(names);
/The output will be ['guru','hemanth','sushan','gaurav'] /
Removing(pop) an Element
Removing or Poping is a method in the array where you can remove or
pop
an element from an array.
var names = ['guru','hemanth','sushan','gaurav'];
names.pop('gaurav');
console.log(names);
/The output will be ['guru','hemanth','sushan'] /
Concatenation
By using concat
we can merge two arrays.
var names = ['guru','hemanth','sushan'];
var ages = [22,32,42];
var concates = names.concat(ages);
console.log(concates);
/The output will be [ 'guru', 'hemanth', 'sushan', 22, 32, 42 ] /
Array Shift
The
shift
keyword removes the first element in an array and shifts all the other elements in an array to a lower index.
var names = ['guru','hemanth','sushan'];
names.shift();
console.log(names);
/ The output will be [ 'hemanth', 'sushan' ] /
Array Unshift
The
unshift
keyword adds the element at first in an array and unshifts the older elements.
var names = ['guru','hemanth','sushan'];
names.unshift('gaurav');
console.log(names);
/The output will be [ 'gaurav', 'guru', 'hemanth', 'sushan' ] /
Array Splicing
Using the
Splice
method in an array we can add or remove an element from an array.
var names = ['guru','hemanth','sushan'];
console.log(names);
names.splice(1,2,'gaurav','wodeyar');
console.log(names);
/ Here in splice 1 is the index where elements should be added and 2 is the value where how many elements should be removed /
output:
[ 'guru', 'hemanth', 'sushan' ]
[ 'guru', 'gaurav', 'wodeyar' ]
Array Slice
The
Slice
method slices out a piece of an array into a new array.
var names = ['guru','hemanth','sushan','gaurav','wodeyar'];
console.log(names);
var slicedNames = names.slice(2);
console.log(slicedNames);
/ Here I have to given slice 2 so that there will be a new array from that index /
output
[ 'guru', 'hemanth', 'sushan', 'gaurav', 'wodeyar' ]
[ 'sushan', 'gaurav', 'wodeyar' ]
Array Sort
Using the Sort
method we can sort the elements in alphabetical order.
var names = ['guru','hemanth','sushan','gaurav','wodeyar'];
console.log(names);
names.sort();
console.log(names);
output
[ 'guru', 'hemanth', 'sushan', 'gaurav', 'wodeyar' ]
[ 'gaurav', 'guru', 'hemanth', 'sushan', 'wodeyar' ]
Array Reverse
Using the Array
Reverse
method we can reverse the array.
var names = ['guru','hemanth','sushan','gaurav','wodeyar'];
console.log(names);
names.reverse();
console.log(names);
output
[ 'guru', 'hemanth', 'sushan', 'gaurav', 'wodeyar' ]
[ 'wodeyar', 'gaurav', 'sushan', 'hemanth', 'guru' ]