12 interesting functions in JS that makes code easier than ever before
JavaScript is a high-level, interpreted programming language commonly used in web development to make web pages more interactive. JS allows you to implement complex features on web pages.Java script makes coding easier than before. Javascript is enriched with a vast collection of different built-in functions, libraries, and tools.
Let's see and learn some easy and useful functions in js.
1.indexOf()
To search and find returns the index number of the searched character or substring within the string. The default value of this function is 0, if that element does not contain. It is very useful to find any expected element easily.
Syntax: indexOf(substr, [start])
2.match(regexp)
Regexp is short-term for the regular expression. It executes a search in a string based on a regular expression. It returns an array of information or null if no match is found.
Syntax: match(regexp)
3.replace function
Searches and replaces the regular expression (or sub string) portion (match) with the replaced text instead.
Syntax: replace(substr,replacetext)
4.Search (regexp)
This function executes to find is there any number contained in a string.If contained it returns the index of the match, or -1 if not found.
Syntax: search(regexp)
5.Slice() Function
This function returns a substring of the string based on the “start” and “end” index arguments, “End” is optional, and if none is specified, the slice includes all characters from “start” to end of the string.
Syntax: slice(start, [end])
6.Every Method
This method examines or checks whether all the elements present in the array satisfy a specified condition by the provided function. It returns a Boolean value.
Syntax:arrayName.every(functionName);
7.shift() function
The shift() method is used to remove the first array element and shifts all other elements to a lower index. It will return you the string that has been shifted out. Shifting is similar to popping, working on the first element instead of the last.
Syntax: arrayName.shift()
8.unshift()
The unshift() method adds a new element at the beginning of an array and unshifts older elements.Unshift() will add the new element into the array and return the length of the new array.,It is similar to Push() and returns the new array length.
Syntax: arrayName.unshift()
9. toString()
It is a built-in function of javascript. This method is used to convert an array to a string or array values, separated by commas saves time.
Syntax: arrayName.toString()
10.toJoin()
In my word, join is an updated version of toString. The join method is used to join all array elements into a string, but in addition, can specify the separator. So, it is more dynamic.
Syntax- arrayName.join(separator)
11.reverse()
The reverse method is a widely used method. Reversing an array wasn’t this much easy ever. This method is used to reverse the order of the elements in an array but it will change the original array and swap the order of the elements.
Syntax- arrayName.reverse()
12. slice()
Don’t get confused, this slice is not slicing a cake, it is slicing an array. This method is used to slice out a piece of an array into a new array. It creates a new array without removing any elements from the source array.