10 Important Topics of Javascript

Plabon Chowdhury
5 min readMay 16, 2021

If you are currently learning javascript, these 10 most used methods are a must-know for you. Let’s dig in together and explore javascript.

1.String.indexOf

indexOf() method is used to get the position of the searchString part inside the main string. it takes a searchString as an argument and returns the current index of that searchString in the main string. indexOf() will return -1 if the given argument is not present in that string. This method is also case sensitive. For example,

The output of this following code will be 14 and -1. In the first case, indexOf() only searched the word once. It will not count the second appearance of that word. For the index of the second appearance, we have to use lastIndexOf() method. And in the second case, JavaScript is written in upper case. So the method couldn’t find the word JavaScript in the string and returned -1.

2. String.split()

split() method converts a string into an array based on a splitting pattern. The pattern Can be ‘/’ , ‘*’ , ‘ ‘, and so on. This method will search for the pattern in the string and divides the string on that point. This process continued till the end of that string. Then it makes an array with those divided parts by counting each part as the array element.

The output will be [“Javascript”, “is”, “mostly”, “misunderstood”, “language”]. Here the search pattern is white space (‘ ‘).

3.String.slice()

slice() is used to cut a string. It takes 2 parameters, the starting index and the ending index of the string. It will cut the string from the starting index to the (end index -1) position.

The output will be ‘Hello W’. slice() started to cut the string from 0 to 6th index. Be careful, not the 7th index.

4.String.startsWith()

startsWith() method takes 2 arguments, the searchString, and the starting position. The second argument is optional. This method will search for the given argument starting from the given position and will return a boolean result. If the string starts with the searched argument it will return ‘true’. Else will return ‘false’.

This will print ‘The result is True’.
endsWith() method works exactly the same as startsWith(). The difference is, endsWith() is used to see if the string ends with the searched argument.

5. parseInt()

parseInt() function parses a string and returns a number. It takes two arguments, the string, and the base or radix. Base or radix is a number that represents the numeral system to be used. It is optional.

It prints ‘Number’ as result. The shortcut method is to use a unary operator (+) before the string and it will be converted to a number!
parseFloat() works in the same way as parseInt(). The difference is it parses a string and converts the string to a floating number.

6.Array.filter()

filter() method takes a callback function as an argument. The callback function has 3 parameters, currentValue, index, array. Then it filters the main array based on the given condition or expression and creates a new array of the filtered result. It doesn’t change the main array. For example,

it will return an array containing the word that has at least 2 letters. output is [ ‘love’, ‘javascript’,‘fun’, ‘coding’ ]. It doesn’t change the main array.

7.Array.pop()

pop() method is used to remove the last element of the array.

output is [1, 5, 6, 7] . It poped the last element from the array.
shift() method is also used to remove elements. But it removes the first element of the array.

8.Array.push()

push() is used to insert a new element in an array. It inserts an element as the last element of the array.

This following code will return an array containing [1, 5, 6, 7, 13].
unShift()
method is similar as push(). It is used to insert an element in the starting of an array.

9.Array.reduce()

reduce() method is used to reduce an array to a single value. It looped through on every element of that array. It has 2 arguments, a callback function, and an initial value. The callback function has 4 parameters: accumulator, currentValue, index, array. The accumulator is the previously returned result of the callback function. On starting, the initial value is counted as the accumulator. The currentValue is the current element value of the array. The rest 2 parameter (index and array) is optional. For example,

The output is 105. Initially, the total is 0 which is the given initial value. The function loop through every element of the array. Takes the currentValue and add it with the total.

10.Array.splice()

There are several ways to cut an array. splice() is one of them. splice() method takes 3 arguments, (starting position ) where the cut will initiate, (deleteCout) the number of elements to remove, (item to insert…..).
splice() method will change the main array. It doesn’t create a new array. For example,

The output is [“jan”, “delete”, “successful”, “april”, “may”]. It starts from the 1 index of the array, deletes 2 elements, and then inserts the given elements in that position. This method permanently changed the main array. So, if you want to cut an array without changing the main array, just use slice() method.

THANK YOU !

--

--