10 Useful JavaScript functions that you must know
JavaScript is a high level, just-in-time complied, server side scripting language. It is the heart of websites. JavaScript is a fun and flexible programming language. It’s one of the core technologies of web development and can be used on both the front-end and the back-end. It has a numerous amount of built-in-functions that comes very handy for a developer. They increase the productivity and optimization of a developer as they reduce the tendency to write long codes and makes the code look cleaned and optimized.
Today we will talk about 10 JavaScript built-in-functions that every JavaScript developer/ web developer must know.
1. Filter():
This is a JavaScript array function. The filter() method iterates through all the elements in an array and creates a new array with all the elements that passes the test implemented by the provided function. filter() calls a provided callback function once for each element in an array, and constructs a new array of all the values for which callback matches the given conditions. The callback function inside the filter() method returns a Boolean value. Its value can either be true or false. The filter() method returns a new array with the elements that pass the test inside the callback function . If no elements pass the test, an empty array will be returned
2. Map():
The map() method is another JavaScript array function that iterates through all the elements of an array and gives back a final result. The map() method creates a new array of the results of calling a provided function on every element in the calling array. It takes a callback function that matches the given conditions with all the elements of the array and makes a new array with the elements that matches with the condition.
3. Reduce():
The reducer() method executes a function (a reducer function that you will provide) on every element of the array and return a single output value. The reducer function takes four arguments:
- accumulator
- current value
- current index
- source array
The accumulator stores the return value of the reducer function and the value iterates for every element of the array resulting in a single value which is the final output of the reduce() method.
4. Sort():
The sort() method sorts the elements of the array and returns the sorted array. the default sorting is in ascending order. It is a destructive function that means it alters the current values in the array and never returns a new array but the existing one.
5. Splice():
The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place. The first parameter is the starting index of the array, the second is the number of delete counts, and the rest of the parameter are the items that should be inserted. Syntax: splice(start, DeleteCount, item1, item2, itemN).
6. abs():
The abs() functions returns the absolute value of a number. As its a static method of Math(), it is always used as Math.abs(). Passing an empty object, an array with more than one member, a non-numeric string or undefined/empty variable returns NaN. Passing null, an empty string or an empty array returns.
There are a whole lot more math functions that does the math operations more easier.
7. parseInt():
The parseInt() method parses a string argument and returns an integer of the specified base. The first parameter is the string that is to be parsed. If the value is not a string than it is converted to a string first using toString() and then parsed into an integer value. the second parameter is the base of the given string.
8. parseFloat():
The parseFloat() method parses a parameter and return a floating point number. If a number cannot be parsed from the parameter then it return NaN. The parameter is the string that is to be parsed. If the value is not a string than it is converted to a string first using toString() and then parsed into an integer value.
9. replace():
The replace() method return a new string with some or all matches of a pattern replaced by a replacement. The replace() function returns a new string and leaves the original string unchanged. The pattern to be replaced can be a string or a RegExp and the replacement can be a string or a function to be called for each match.
10. split():
The split() method divides a string into many substrings and then puts the substrings into an array and returns the array. The division is done by searching for a pattern and wherever the pattern is matched the string is divided into a substring and stores in a new array.
JavaScript has a lot of built-in-functions that gives developers al slight relief as they do not have to write all the long codes. Every developer must have a proper knowledge in this functions.