Learn about javascript Array Mutators
Posted By : Vinay Tiwari | 27-Dec-2017
In this blog, we are going to be learning about mutator methods in javascript. Various methods are there which can be used as the array object in javascript. A few methods modify the array object whereas the others do not. The methods which modify the array objects are called as mutator methods.
Here are some examples of non-mutator methods
- contains indexOf, lastindexOf
for example, Contains method checks if a given array object contains a given element or not. If this array contains a given element then the contains methods would return true otherwise false. This method does not change the array object on which they operate.
Here are the examples of mutator methods - push, pop, shift, unshift, reverse, sort, splice
Here, the second set of methods changes the array object on which they operate. These methods are known mutator methods.
Now, we will define all mutator methods
push() method: This adds new items at the end of the array, also it changes the length of the array.
Now, will define all mutator methods
var numbers = [1,2,3,4];
numbers.push(5);
console.log(numbers); // [1,2,3,4,5]
pop() method: This removes the last element of an array, and returns that element, it changes the length of an array.
var numbers = [1,2,3,4]
numbers.pop();
console.log(numbers); // [1,2,3]
unshift() method: push() method, adds new items at the end of the array. To add new items at the beginning of the array, then use unshift() method. Same as push() method, unshift() method also changes the length of an array.
var numbers = [6,7,8];
numbers.unshift(4, 5);
console.log(numbers); // [4, 5, 6, 7, 8]
shift() method: pop() method, this method removes the last element of an array, and returns that element whereas the shift() method removes the first item of an array and returns that item. Same as pop() method, shift() method also changes the length of an array.
var numbers = [1,2,3];
numbers.shift();
console.log(numbers); // [2,3]
sort() method: This method sorts the elements in an array. By default, the sort() method sorts the values by converting them to string and then comparing those strings which
var myArray = [sam, Mark, Tom, David];
myArray.sort();
console.log(myArray); // [David, Mark, Tom, sam]
Sorting Numbers
var myArray = [20, 1, 10, 2, 3];
myArray.sort();
console.log(myArray); // [1, 10, 2, 20, 3]
Do make sure to notice that the numbers are not sorted properly. This can be fixed by
var myArray = [20, 1, 10, 2, 3];
myArray.sort(function (a, b) { return a - b });
console.log(myArray); // [1, 2, 3, 10, 20]
reverse() method: Reverses the order of the elements in the array.
var a = [1, 2, 3, 4];
a.reverse();
console.log(a); // [4, 3, 2, 1]
splice() method: This method adds or removes elements from an array.
These method has got three parameters (index, deleteCount, item1,...,
Explanation of these parameters:
the index parameter is required and mentions the position
var myArray = [1, 2, 5];
myArray.splice(2, 0, 3, 4);
console.log(myArray); // [1, 2, 3, 4]
Explanation of splice() method
There are three elements [1, 2, 5] and those are to be added to 3, 4 of this array. In this array, two elements [3, 4] are added at the index position of 2.
(myArray.splice(2, 0, 3, 4);), Here in the index 2 & second parameter is deleteCount, if one would we want to delete an element from the array, then pass 0 there and the third parameter is item1 to
Thanks
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Vinay Tiwari
Vinay is a bright UI developer, having knowledge of HTML, CSS, Bootstrap, Jquery and AngularJs. His hobbies are interacting with people, listening music etc.