Javascript Arrays And its Methods

Javascript Arrays And its Methods

Arrays are the most powerful and useful datatype in javascript. It has many methods which make the life of a programmer easy.

First of all, arrays are object which is used to store multiple values in a single variable. It's index starts from zero and uses numbers to access its elements of type string, number. objects, boolean and other.

Different methods of Array

1. length()

It is used to find the total length of the array.

const  country = ['india', 'usa', 'canada', 'australia'];
console.log(country.length);    //4

2. Accessing and modifying array items:

  • For accessing an array we are using array name with index value. eg. a[0]
const  country = ['india', 'usa', 'canada', 'australia'];
console.log(country[0]);   //india
  • For modifying an array item we use array[i] and update that element
const  country = ['india', 'usa', 'canada', 'australia'];
country[1] = 'singapore';
console.log(country[1]);   //singapore

3. push()

This method adds provided elements at the end of an Array. you can pass multiple parameters in push() method.

country.push("new zealand")
console.log(country);
//[ 'india', 'usa', 'canada', 'australia', 'new zealand' ]

4. pop()

This method delete an element at the end of the Array.

country.pop();
console.log(country);
// [ 'india', 'usa', 'canada', 'australia' ]

5. shift()

This method is used to remove an element from starting of the array.

country.shift();
console.log(country);
//[ 'usa', 'canada', 'australia' ]

6. unshift()

This method is used to add a new element from starting of the array.

const  country = ['india', 'usa', 'canada', 'australia'];
country.unshift('India');
console.log(country);
//[ 'India', 'usa', 'canada', 'australia' ]

7. splice()

This method is used to change contents of an array by removing or replacing an elements.

const  country = ['india', 'usa', 'canada', 'australia'];
country.splice(1,2, "spain", "ireland");
console.log(country);
// [ 'India', 'spain', 'ireland', 'australia' ]

8. slice()

It returns a new array coping to it all items from index start to end

const  country = ['india', 'usa', 'canada', 'australia'];
const newarr = country.slice(1,3);
console.log(newarr);
// [ 'spain', 'ireland' ]

9. concat()

This method accepts an array or value in argument and return a new single array containing both previous array and new value.

const  country = ['india', 'usa', 'canada', 'australia'];
const states = ["rajasthan", 'gujarat']
console.log(country.concat(states));
// [ 'India', 'spain', 'ireland', 'australia', 'rajasthan', 'gujarat' ]

10. sort()

This method is used to sort an array in assecending order. notice here, it will also sort first uppercase element and then lowercase.

const  country = ['india', 'usa', 'canada', 'australia'];
console.log(country.sort());
// [ 'India', 'australia', 'ireland', 'spain' ]

11. reverse()

This method is used to reverse the array.

const  country = ['india', 'usa', 'canada', 'australia'];
console.log(country.reverse());
// [ 'spain', 'ireland', 'australia', 'India' ]

12. indexOf()

when we want to find the index of an element then we use this property.

const  country = ['india', 'usa', 'canada', 'australia'];
let search = country.indexOf('India');
console.log(search);
//3

13. copywithin()

This method is used to copy an element within an array

syntax: copyWithin(target, start index, end index)

const arr = ['a', 'b', 'c', 'd', 'e'];
console.log(arr.copyWithin(0, 3, 4));
// ["d", "b", "c", "d", "e"]