Array and Array Method in JavaScript
Quick overview of Array and Array Method in JavaScript
Table of contents
What is Array
Arrays in JavaScript is an object which is used to store multiple values having the same or different data types in a group. That means we can store elements of type Number, Boolean, String, Object, etc.
Arrays are represented by a pair of square brackets []
. The values in an array are known as elements and their position is defined by index ( or indices). Each element in an array is separated by commas (,
).
var animals = ["dog" , "cat" , "elephant" , "cow" , "gorilla"];
console.log(animals);
//output
["dog" , "cat" , "elephant" , "cow" , "gorilla"]
Now Lets see how we can access single value under Array. Array values are accessed by its Index Number. Index number of Array starts with 0.
var animals = ["dog" , "cat" , "elephant" , "cow" , "gorilla"];
console.log(animals[0]);
console.log(animals[1]);
console.log(animals[2]);
console.log(animals[3]);
console.log(animals[4]);
//output
dog
cat
elephant
cow
gorilla
Array Method in JavaScript
Array Length Method
Array Length method used to find length of array elements by this method we can know what is length of array.
Lets understand with good example
var animals = ["dog" , "cat" , "elephant" , "cow" , "gorilla"];
console.log(animals.length);
//output
5
Array indexof() Method
This method is used to find the index of an element in an array. If an element is not present then indexOf() method returns -1
Lets understand with good example
const groceryList= [ 'Egg', 'Butter', 'Bread', 'Cereals', 'Bread' ];
groceryList.indexOf('Bread'); // returns 2
groceryList.indexOf('Rice'); // returns -1
Array reverse() Method.
This method reverses the position of all the elements in an array. This method modifies the original array.
Lets understand with good example
const groceryList= [ 'Egg', 'Butter', 'Bread', 'Cereals' ];
groceryList.reverse();
//output
[ 'Cereals', 'Bread', 'Butter', 'Egg']
Array sort() Method.
This method first converts all the elements of the array into a string and then sorts them in ascending order. This method
Lets understand with good example
const groceryList= [ 'egg', 'butter', 'bread', 'cereals', 'rice' ];
groceryList.sort();
//output
[ 'Bread', 'Butter', 'Cereals', 'Egg', 'Rice' ]
Array toString() Method
Array toString() method converts array to string
Lets understand with good example
var animals = ["dog" , "cat" , "elephant" , "cow" , "gorilla"];
console.log(animals.toString());
//output
dog,cat,elephant,cow,gorilla
Array join() Method
Array join() method converts array to string , but only change is we can add separator.
Lets understand with good example
var animals = ["dog" , "cat" , "elephant" , "cow" , "gorilla"];
console.log(animals.join(" - "));
//output
dog - cat - elephant - cow - gorilla
Array pop() Method
Array pop() method removed last element from array.
Lets understand with good example
var animals = ["dog" , "cat" , "elephant" , "cow" , "gorilla"];
animals.pop();
console.log(animals);
//output
[ 'dog', 'cat', 'elephant', 'cow' ]
Array push() Method
Array push() method add last element in array.
Lets understand with good example
var animals = ["dog" , "cat" , "elephant" , "cow"];
animals.push("gorilla");
console.log(animals);
//output
[ 'dog', 'cat', 'elephant', 'cow', 'gorilla' ]
Array shift() Method
Array shift() method remove first element from array.
Lets understand with good example
var animals = ["dog" , "cat" , "elephant" , "cow"];
animals.shift();
console.log(animals);
//output
[ 'cat', 'elephant', 'cow' ]
Array unshift() Method
Array unshift() method add first element in array.
Lets understand with good example
var animals = [ "cat" , "elephant" , "cow"];
animals.unshift("dog");
console.log(animals);
//output
[ 'dog', 'cat', 'elephant', 'cow' ]
Array splice() Method
Array splice() method is used to add element in array. There are two parameters while defining 1st parameter is for position where it should be added whereas 2nd parameter is for how may elements should be removed.
Lets understand with good example
var animals = [ "cat" , "elephant" , "cow"];
animals.splice(2 , 1 , "dog");
console.log(animals);
In animal.splice() : 2 means dog will added in index number 2 1 means how much elements should be removed.
//output
[ 'cat', 'elephant', 'dog' ]
Array slice() Method
Array slice() method is used to slice an array into pieces and create a new array slice() method has two parameters from which index number need to sliced out till last limit of element.
Also we can give 1 parameter suppose in array there are 5 values and given one parameter is 1 so from first index number all values will sliced out.
Lets understand with good example
var animals = [ "cat" , "elephant" , "cow" , "lion" , "Tiger"];
console.log(animals.slice(2, 4));
In console statement 2 value means from index number 2 it will start till 4th Index all values will be sliced out and print
//output
[ 'cow', 'lion' ]
Array concat() Method
Array concat() method is used to concat if there are two or more arrays presented.
Lets understand with good example
var animals = [ "cat" , "elephant" , "cow" , "lion" , "Tiger"];
var food = ["Wheat" , "rice" , "milk"]
console.log(animals.concat(food));
Animals array is concatenated with array food
//output
[ 'cat', 'elephant', 'cow', 'lion', 'Tiger', 'Wheat', 'rice', 'milk' ]