JavaScript methods you should know about

Posted By : Gaurav Kumar | 26-Apr-2020

javascript

Loading...

Javascript methods

In this particular blog, let's talk about some traditional but relevant javaScript methods that everyone should know about.

javaScript Methods

forEach: This method allows you to iterate through all the elements present inside the array, pretty similar to forEach of other programming languages.

example:

array.forEach(function(currentValue, index){

/**

* stuff to perform with each record

*/

});

map: This method returns a new array by calling the provide method once for each element present inside the array in the order in which they stored. It does execute the provide method for empty value of the array.

example :

array.map(function(element){

/**

* stuff to perform to each element of array

*/

});

filter: This method returns an array that passes the condition that matches into the method provided, and executes the provide method for empty value of the array.

example :

array.filter(function (element) {

` /**

* filter condition and return data logic

*/

});

reduce: This method takes each element of array and the last calculated value started from left most index of array and returns a final single value based on the action performed in the method provided.

example :

array.reduce(function(finalVal, currentVal) {

/**

* calculation on final and current value of array

*/

});


reduceRight: This method is similar to reduce, the key difference is that it starts evaluation from right most index of array.

example :

array.reduceRight(function(finalVal, currentVal) {

/**

* calculation on final and current value of array

*/

});


join: As the name suggest, this method joins each entries into array as a string based on the separator provided. The default separator is a comma.

example :

array.join('-');


splice: This method adds/removes elements to/from an array based on the param provided.

parameter description : array.splice(index, times, element1, ....., elementN)

index: Required int param indicating the position where elements can be added/removed. We can use a negative value to mention index from the last index.

times: Optional param indicating the number of elements to be removed.

element1,...,elementN : Optional param indicating the items to be added into array.

includes: This method takes elment as a param and tests its presence into an array and returns boolean value accordingly. Note that element comparison is case sensitive here.

example :

array.includes(elementToTest);

The most widely used programming language in web and mobile applications, javaScript is a light-weight clientside scripting language that our developers hold expertise in. Are you looking for development services to design web applications and build backend for your server? Look no further! We are an ERP Development company with expertise in developing customized software solutions for our clients. Our custom development services enable businesses to automate, plan, collaborate, and execute their operations methodically.