Built-in Methods in JavaScript

Lamisa Zamzam
5 min readMay 5, 2021

String Methods in JavaScript

String.prototype.charAt():

This method returns a new string that consists of a single UTF-16 code unit located at the index that you specify in the brackets of the method. This index can be any integer from 0 to 1 less than the length of the string. If you provide an index that doesn’t exist in the string, it returns an empty string. And if no index is provided, its value is set to 0 and returns the first character of the string.

String.prototype.concat():

The concat() method appends the strings past as parameters serially to the string that calls it, makes a new string, and then returns that new string. If the arguments provided are not strings, they are converted to strings and then concatenated to the calling string.

** concat() doesn’t provide a great performance. Try to use + or += instead.

String.prototype.includes():

The includes() method looks for a string within another string. It is case-sensitive. If the argument passed to it exists in the string which calls it, it returns true; otherwise false.

Includes() can have another optional parameter. The second parameter is an integer and indicates the index of the calling string from which the search for a string should begin. Its default value is 0; which means it starts searching from the beginning.

Includes() method is new and was introduced the first time in ECMAScript 2015. So every browser doesn’t support it yet.

String.prototype.endsWith():

The endsWith() method determines whether a string ends with the characters of a specified string. It returns true if the string ends with that specified string; otherwise false. It is case-sensitive.

This method can have an optional second parameter as indexOf(). Here this parameter is the integer that specifies the length of the string. Its default value is the string’s length.

**As this method was specified in ECMAScript 6, it might not be available everywhere. In case you need it, you can polyfill.

String.prototype.indexOf():

This method returns the index of the first occurrence of a specified value in a string. If no parameter is provided, the value to be searched is set to “undefined”, and then it searches if “undefined” is present in the string. If it doesn’t find that value in the string, it returns -1.

It can also have an optional second parameter that specifies the index from which the search should begin. In case it is less than 0, it starts searching from index 0 and if it is greater than the length of string, search is started from the index equal to the length of the string.

An empty string as a search value is an interesting one here. It returns the value of the second parameter and if not specified, 0. In case the value of the second parameter is equal to or greater than the length of the string, it returns the length of the string.

String.prototype.lastIndexOf():

This method searches backward and returns the index of the last occurrence of a specified value in a string. If it doesn’t find that value in the string, it returns -1. It is case-sensitive.

It can also have an optional second parameter that specifies the index of the last character from which the search would continue. In case it is less than 0, it starts searching from index 0 and if it is greater than the length of string, the whole string is searched. Its default value is +Infinity.

Number Methods in JavaScript

Number.isNaN():

This method checks if the value passed to it is NaN(Not a Number) and its type is Number, and if it is, returns true; otherwise false. It is a special situation in JS and doesn’t forcefully convert the type of the parameter to a number like the global isNaN() function, and this is why it is preferred.

It will return false in most cases. Some cases in which it will return true is when NaN, Number.NaN or 0/0 is passed as a parameter.

Array Methods in JavaScript

Array.prototype.reverse():

This method reverses an array, which means makes the serial of the array elements reversed. reverse() can be used for objects also, but objects which do not have a length property will behave unexpectedly. If any object is such that its properties are like the indices of an array( starts from 0, then comes 1 and thus continues serially), whatever the value its properties contain, it can be reversed using the reverse() method. It reverses the original object or array.

Array.prototype.sort():

The sort() method sorts the elements of an array and returns the sorted array. The original array is also changed. It sorts the elements in an ascending order converting the elements into strings and comparing their sequences of UTF-16 code unit values. This is very complex and depends on all the values provided.

This method can have three parameters. The first optional parameter is a function to define the sort order of the elements, which if not provided will follow the pattern described above. Note that if you want to sort numbers by their numeric ascending order, you have to provide this function with two parameters and return the first value minus the second value.

The second and third parameters of the sort() function are two elements to be compared. If you want to sort the elements of an array, you just write the array or the name of the array, then write a full-stop (.) and finally sort().

Array.prototype.every():

The every() method tests if all the elements of an array pass the check of the provided callback function. It calls the function once for every element of the array until the function returns a falsy value for one element. As soon as a falsy value is returned from the callback function, it returns false and returns true only when for every element of the array, the callback returns truthy value. Calling this method on an empty array always returns true.

It has a callback function as a parameter, and a thisArg parameter optionally, which use used as “this“ when executing the callback function. The callback function must have an element parameter, and two other optional parameters (index of the element in the array and the array upon which it was called).

Default object Math methods in JavaScript

Math.sqrt():

Math is a built-in object of JS with properties and methods for mathematical constants and functions which works with Number type, and not with BigInt. Sqrt stands for SQuare RooT. The Math.sqrt() function calculates the square root of the parameter passed and then returns that value. It has only one parameter which is the number to be square rooted and if this value is negative, NaN is returned; because negative values never have a square root — Math is not a constructor, and sqrt() is a static method of it.

Math.abs():

The Math.abs() function calculates the absolute value of the number passed as its parameter and returns that absolute value(non-negative value). If the number passed is positive or zero, it returns exactly the number passed and if the value is negative, it returns the negation of the number passed. It has only one parameter that is the number the absolute number of which will be calculated. For any parameter whose type is not number, it returns NaN.

--

--