Commonly utilized JavaScript methods Part I
In this entry we're going to be covering commonly utilized JavaScript methods, their uses, optimal data structures to append them to and essentially obtain a greater understanding of the language of JavaScript via the intricacies of these methods. The types of methods I'm going to be covering in this series are Mutator Methods, Accessor Methods and Iteration Methods. This article shall particularly cover the Mutator Methods as it is part I of Commonly utilized JavaScript Methods
What are Mutator Methods?
Mutator methods are essentially utilized to manipulate arrays. Mutator methods are also identified as Setter method, which is quite often accompanied with Getter methods also known as Accessor methods. Commonly used methods associated with Mutator methods are: .unshift(), .shift(), .push(), .pop(), .reverse(), .sort(), .splice(), .fill(), and forEach(). Below I shall display the various definitions, uses, and examples of the Mutator Methods identified above.
.unshift():
This method is responsible for adding new elements to the beginning of an array, furthermore overwriting the original array and returning the new length of the array.
This is an optimal method to add a new element, in an array:
Example of .unshift() method:
.shift():
This method is responsible for removing an element from the beginning of an array as opposed to the unshift method above.
Example of .shift() method:
This method is similar to the unshift method and does the opposite, instead of adding an element or sets of elements to the beginning of the array this method is responsible for placing elements at the end of the array.
Example of .push() method:
.pop():
This element removes the last element of the array furthermore, manipulating the length of the original array.
Example of .pop() method:
.reverse():
This method just as expected reverses the array.
Example of .reverse() method:
.sort():
This method like its name is responsible for sorting the elements in an array, this can be pertaining to alphabetically, or ascending and descending.
Example of .sort() method - Alphabetically:
.splice():
This method is responsible for the manipulation of an array, it add or removes an element from an array and overwrites the original. This is done via removal/replacing elements. It is done so via passing parameters. The initial parameter is the start which is responsible for placing the first index to manipulate in the array. The second parameter is identified as the deleteCount this is responsible for the mitigation of how many elements to be removed in an array.
Example of the .splice() method:
.fill():
This method manipulates array by changing elements to a static value via passing of parameters: value, start and end.
.forEach method:
This method is integral for the iteration of an array, via use of a callback function and the passing of 3 parameters: Current Value, Index and Array. Current value is the only parameter that is typically required, it was required to produce the current value of array element, Index returns the index number, Array returns what array the current element derives from.
Example of .forEach() method via current value:
This concludes my article regarding the commonly utilized JavaScript methods, be sure to tune in for part two of the methods covering Accessor Methods.
Comments
Post a Comment