Javascript Basic Array Methods

Array Object

The array object in javascript is used globally. Using an array, we can store multiple values in one. We will see basic array methods and properties one by one:

Properties

  • Length - This property is getter/setter for the number of elements in the array.
  • Prototype - This property is associated with the Array object to add new values or properties to the Array object.

Methods

We will see the basic methods to insert and delete from an array. Suppose we have an array named arr. So we will see methods work:

Push() - This method is used to add elements at the end of the array. We can perform certain actions after creating an array in javascript. This is because array objects are mutable in nature. We can perform manipulations such as changing values, adding, removing elements from the array. This method will return the new length of the updated array after inserting the element.

var arr = [1, 3, 5, 7];
 arr.push('aanchal');
console.log(arr); // Prints [1, 3, 5, 7, "aanchal"]

var array = ['ananya', 'bala', 'chirag'];
var b = array.push('ward');
console.log(array); // Prints  ["ananya", "bala", "chirag", "ward"]
console.log(b); // Prints 4 (updated length after inserting element)


Unshift() - This method is used to add elements at the start of the array. This method will return the new length of the updated array after inserting the element. 

var arr = [1, 3, 5, 7];
 arr.unshift('aanchal');
console.log(arr); // Prints ["aanchal", 1, 3, 5, 7]

var array = ['ananya', 'bala', 'chirag'];
var b = array.push('ward');
console.log(array); // Prints  ["ward", "ananya", "bala", "chirag"]
console.log(b); // Prints 4 (updated length after inserting element)

Pop()This method is used to remove elements from the end of the array. This method will return the removed element itself.

var arr = [1, 3, '11', 5, 7];
var pop = arr.pop();
console.log(arr); // Prints [1, 3, "11", 5]
console.log(pop); // Prints 7 (popped element)

Shift()This method is used to remove elements from the start of the array. This method will return the removed element itself.

var arr = [1, 3, '11', 5, 7];
var pop = arr.shift();
console.log(arr); // Prints [1, 3, "11", 5]
console.log(pop); // Prints 1 (popped element)

Splice() -  We can insert and delete elements from any index using this method. We need to specify the index from where we need to remove elements, counter to delete elements, as well as new elements to be added into the array. This method returns the removed items.
It has the following signature:

arr.splice(index, counter, e1, e2 ....., en);
  • index - this is the required entry. We need to specify integer value in which index position we need to add or remove the elements. Generally, it will add or remove from the starting of the array. If we specify a negative value for this, it will be from the end of the array.
  • counter - this is an optional field. If we will not specify this field, it will remove all the elements starting from the specified index position. This field is for the number of elements we want to remove. If we set it to 0, then no element will get removed.
  • e1, e2,..... en - this is also optional. The new elements which we want to add to the array. 

var arr = [1, 3, '11', 5, 7];
var pop = arr.splice(3);
console.log(arr); // Prints [1, 3, "11"]
console.log(pop); // Prints [5, 7] (removed items)

Now we will see an example where we will remove as well add new elements using splice().

var array = ['snighdha' , 'radha', 'sudha', 'shiva', 'diwakar']; // Prints ["snighdha", "radha", "jasmine", "diwakar"]
array.splice(2,2,'jasmine'); /
console.log(array);

array.splice(2,2,'jasmine') means 2 is the index from where we need to remove elements and the second field is counter which means we need to remove a total of 2 elements starting from index 2 and insert a new element at that position.

Comments