Some important methods of string you need to know to start with JavaScript

Tanvir Ahmed
4 min readMay 5, 2021

In JavaScript, string manipulation and modification is a key topic that every developer should learn before going through the higher level of this programming language. In computer programming, a string is a sequence of characters, that can be literal constant or variable. In this post, we simply will go through some of the main methods that have a great number of uses in JavaScipt.

1. charAt()

charAt() is a method in a string that returns a new string with the value of a certain index that is included as a parameter of the method. If the index is not given as a parameter it gives the result for the 0th index which is the very first character of the string.

2. concat()

This method concatenates multiple string arguments with each other and returns a new string with concatenated argument. It works like a chain of strings.

3. endsWith()

This method applies when you need to check if a defined string ends with the given values of the specified string. It takes two parameters one is the specified string and the other one is the index of the defined string.

4. includes()

includes() is a method by which one can determine if on a given string the value of a specified string is present. The output will be boolean values true or false.

5. indexOf()

This is a method that returns the index value of a specified element in a string. It counts the first occurrence of the specified element which is given as the parameter of the method. It also takes the second parameter which includes the start index of this method. If the second parameter not given it will 0 as default. If the element is not found in the string it will return -1.

6. lastIndexOf()

It is almost as same as the previous method indexOf() the difference is it only searches for the last occurrence of a specified element of a string. It also gives the output -1 if the searched element is not found throughout the string.

7. replace()

This method replaces the substring of an initial string with a new string that is given as a parameter of the method. It doesn't change the initial whereas it returns a new string with the changes included. The first occurrence of the matched string only will be replaced by the string that is given as a parameter. This method also takes a regular expression as a parameter.

8. startsWith()

This method checks if a string starts the specified string which is given by the parameter of the method. It simply returns true or false after checking if the parameter string is present on the start position of an initial string. It takes two parameters one is the string and another one is the index from where it will start the search. This method is case-sensitive.

9. slice()

The slice() slices the initial string in a certain portion and returns a new string. This method takes two parameters one is the start index from where the resulted string will start and another one is the end index till where the resulted string will be sliced. This method doesn't make any change in the initial string it returns a new string instead.

10. split()

This method divides a string into substrings and stores them as elements of an array. The process includes a separator which comes from the argument of the method in terms of which the initial string will divide into substrings. The method will search for the separator argument whenever it finds an occurrence of that it will make a new substring from the next index. And this method returns an array which is the collection of those substrings.

11. substr()

This method returns a substring from an initial string. It takes two parameters which are the starting and ending index of the initial string from which a new string will produce. If there is one value as an argument it will count as starting index and the ending index will be the last element of the initial string.

12. trim()

This method removes all whitespace from the start and end of a given string. Here whitespace includes space, tab, no-break space, etc.

13. match()

The match() method take a regular expression as an argument and check which portion of a given string is matching with the regular expression. The collection will be substrings and they are the element of the resulted array.

14. toLowerCase()

This method converts all the upper case alphabet of a certain string to a lower case alphabet which denoting on the name itself.

15. toUpperCase()

This method converts all the lower case alphabet of a certain string to an upper case alphabet which denoting on the name itself.

--

--

Tanvir Ahmed

A web developer with a strong interest in projects that require both conceptual and analytical thinking. I'm always eager to learn from anyone and everyone.