DEV Community

Cover image for (Array Methods Part 1) What are Mutator Methods?
MichaelVini
MichaelVini

Posted on • Updated on

(Array Methods Part 1) What are Mutator Methods?

When we started to learn javascript anytime we will meet the famous array methods. Array methods can be separated in three types:

  • Mutator Methods
  • Accessor Methods
  • Iteration Methods

I will create one post for each array type methods, but today I will explain about the first type.

Mutator Methods

Mutator methods besides return a value, they also change original array, such as:

.sort() - sort method organize array in alphabetical order.

The return of this method is the array itself with the changes.

const animals = ['dog', 'cat', 'turtle', 'bird'];
animals.sort();
console.log(animals) // output: ['bird', 'cat', 'dog', 'turtle']
Enter fullscreen mode Exit fullscreen mode

Some others examples:

.push() - push method insert elements into the end of an array

This method change the original array and return the new length, so we can classify as mutator method.

const animals = ['dog', 'cat', 'turtle'];
const count = animals.push('bird');
console.log(count); // output: 4
console.log(animals); // output: ['dog', 'cat', 'turtle', 'bird']
Enter fullscreen mode Exit fullscreen mode

There are a lot of types of mutator methods and if I explain about all methods I think this post will be very big, but that's not my intention, so I will list some important mutator methods that I think you should be study:

  • .unshift()
  • .shift()
  • .pop()
  • .reverse()
  • .splice()
  • .fill()

See you soon!!!

Image description

Top comments (0)