Weeks ago I create a post to explain about types of array methods, I started talking that there are three types of array methods: Mutator Methods, Accessor Methods and Iteration Methods. In case you haven't seen the last post I recommend you see in What are Mutator Methods?.
Today I will explain about second type of array methods.
Accessor Methods
The difference between accessor methods and mutator methods is that accessor methods not change the original array, they return a new array with the changes, for example:
.concat() - This method is used to merge two or more arrays
The return of this method is a new array with all values of array that were merged.
const animals1 = ['cat', 'dog'];
const animals2 = ['fish', 'frog'];
const animals3 = animals1.concat(animals2);
console.log(animals3);
// expected output: Array ['cat', 'dog', 'fish', 'frog']
.filter() - Method is used to filter an array based on the test implemented by the provided function.
The return of this function is the elements that pass in the test implemented by the provided function.
const animals = ['cat', 'dog', 'turtle', 'fish', 'duck'];
const result = animals.filter(animal => animal.length > 3);
console.log(result);
// expected output: Array ['turtle', 'fish', 'duck']
.includes() - Includes method determines whether an array includes a value and returns true or false as appropriate.
const pets = ['cat', 'dog', 'bat'];
console.log(pets.includes('cat'));
// expected output: true
console.log(pets.includes('turtle'));
// expected output: false
Some others important Accessor Methods that I think you should know:
- .join()
- .slice()
- .indexOf()
- .lastIndexOf()
- .toString()
That's all for today!!
Top comments (3)
Waiting for the last one. Great job...
Thank you, the last part is available What are Iteration Methods? I hope you enjoy!
Great Job, tks!