10 String Methods that every JavaScript Developer definitely need to know

Md. Habibur Rahman
5 min readSep 21, 2020

In this article we will learn 10 string methods that you definitely need to know as a JavaScript developer. In the beginning, lets learn what is string in JavaScript or any other programming language.

Definition: String is a collection of character’s which written in quotes.

Example:

let greeting = “Hello, JavaScript developer!”;

String would be numbers, letters or any other special characters but it should be written inside quotes. We won’t go depth of string definition our focus is learn string methods.

1st Method: charAt

chartAt method returns the specified character at index. charAt method takes one parameter which is the position number you are looking for character. String is zero based so counting starts from zero.

2nd Method: concat

concat method returns a string which contains the concatenations of two or multiple strings. concat takes multiple parameter you can pass any number of parameter you want like.

You can write like as below

3rd Method: includes

includes method returns a Boolean which means true or false. If the word is present there it will return true if not returns false.

In includes method you can pass two parameter first parameter is the string you looking for and second number is the starter index like as below

Here we can see it gives us false because it could not found our string after index 30. because index of “word” is 22. If we pass 22 or less it will gives us true. On the other hand if we pass 23 or higher it will returns false.

4h Method: indexOf

indexOf method is more valuable method than includes method. indexOf method search for a substring from a string and returns the position number of first occurrence. If the sting is not found it returns -1 which means the substring is not found.

5th Method: lastIndexOf

lastIndex of method works as same as indexOf method. It is the reverse process. It returns last occurrence of the substring.

6th Method: slice

If you want a small part of your sting you can use slice method. Slice method returns specified portion from the sting. It takes two arguments “inclusive”, “exclusive” which means starter index and ending index.

7th Method: Modify your String: toLowerCase, toUpperCase

If you want to modify your string from upperCase to lowerCase or lowerCase to upper you will use toLowerCase for making all small characters of your string and toUpperCase make all capital characters of your sting. Both method returns a new string either all capital characters or all small characters.

8th method: trim

If you want to removes unnecessary white spaces from the beginning or ending you will use trim method. trim method returns the sting without white spaces from starting of sting and ending of string. If you want to validate your form and if you want to no white spaces from beginning or ending to your input field you can use trim method. which is really helpful.

You can use trimLeft(), trimRight(), trimStart(), trimEnd(). They works as same.

9th Method: split

split method is really helpful if you want to separate your sting into individual entities into a array. You can pass numbers, character, special characters, space to separate your string . split method works best if you want breakup your data.

10th Method: search

search method finds the first substring and match in a regular expression. If searching arguments is matched with the sting it returns the first index number of matched string. If it wont matched returns -1.

Conclusion

There are more string method are there this are the most useful method I discussed. If you have any question feel free to comment below. Happy Programming.

Thanks!

--

--

Md. Habibur Rahman

Hello there! I am a Front-End Developer. I have great passion in React and JavaScript I love to learn new technology, programming languages and frameworks.