tutorialstonight logo
A Platform To Learn To Code

JAVASCRIPT STRING METHODS



What Are String Methods In Javascript?

Javascript String methods are special build-in function which has a specific task. These methods return a new string based on the methods applied without modifying the original string.

Any simple string like "Brenden Eich" is a primitive value, so it does not have any methods or properties with it but when javascript treats these primitive values as objects when we execute it with some method or properties.

Here is a simple example of a string method, find the position of "Eich" in the string.

Test String = "Brenden Eich"

Using indexOf() method


Javascript String Length

Apart from multiple javascript string methods, there is also a string property to find the length of the string.

To find the length of the string used length property on the string, it return number of characters in the string including spaces.

Example

Try It

Javascript String Methods List

Here is a list of javascript string methods, we will go in detail with each methods further in the article. Click on any method to jump to the section.

  1. charAt()
  2. charCodeAt()
  3. concat(str1, str2, ...)
  4. includes()
  5. endsWith()
  6. indexOf()
  7. lastIndexOf()
  8. match()
  9. matchAll()
  10. repeat()
  11. replace()
  12. replaceAll()
  13. search()
  14. slice()
  15. split()
  16. startsWith()
  17. substr()
  18. toLowerCase()
  19. toUpperCase()
  20. toString()
  21. trim()
  22. valueOf()

Now let's look at each String methods in detail.


1. charAt in javascript

The charAt() string method in javascript returns a character at the specific index of the given string.

The charAt() method takes an argument which is index value. An index value of string starts with 0, means the first character has index value 0, the second character has index value 1, and so on.

The default index value is 0. If the index value is out of range then this method returns an empty string.

Example

Try It

2. charCodeAt in javascript

The charCodeAt() string method in javascript returns the Unicode value of the character present at the given index of the string.

The charCodeAt() method takes an argument which is index value. Default index value is 0.

If the index value is out of range then this method returns NaN.

Example

Try It

3. concat in javascript

The concat() string method in javascript concatenates the passed string or strings to the calling string, and return a new string.

The concat() method can take any number of strings as an argument.

If the passed argument is not a string then it converts it to string and then concatenates it.

Example

Try It

Note: for concatenation of string it is suggested to use + or += instead of the concat method.


4. includes in javascript

The includes() string method in javascript determines whether the string in the argument is present in calling string.

If the string is present then the method returns true, if the string is not present then it returns false

The includes() method takes an argument which is index value. The default index value is 0.

The string matching of includes() method is case-sensitive.

Example

Try It

5. endsWith in javascript

The endsWith() string method in javascript determines whether the string ends with a specified string or not. If it ends with specified string then it returns true, else returns false.

The string to be checked for the ending is passed as an argument.

There is another length argument, which is optional. It specifies the length of the string to be checked for.

Please note that endsWith() method is case sensitive.

Example

Try It

6. indexOf in javascript

The indexOf() string method in javascript searches the first occurrence of a character or substring into a string.

If character or substring is present in the string then it returns the index of the first occurrence of it, if it is not present then it returns -1.

If no character or substring is passed then default search is for 'undefined'. Example 'undefined'.indexOf() will return 0.

There is an optional argument which defines the start index from where it starts in the string.

Example

Try It

7. lastIndexOf in javascript

The lastIndexOf() string method in javascript searches last occurrence of a character or substring into a string by searching the substring from backward of the string.

If character or substring is present in the string then it returns the index of the last occurrence of it in the string, if it is not present then it returns -1.

If no character or substring is passed then default search is for 'undefined'. Example 'undefined'.lastIndexOf() will return 0.

There is an optional argument which fromIndex. Its default value is +Infinity. If fromIndex >= str.length then whole string searched.

Example

Try It

8. match in javascript

The match() string method in javascript use regex to match the string. It returns the output as an array with matched character or string as its element.

If parameter passed in not regular expression then it is implicitly converted to RegExp by using new RegExp(regexp).

Example

Try It

9. matchAll in javascript

The matchAll() string method in javascript returns an iterator to all results matching the string against regex.

Example

Try It

10. repeat in javascript

The repeat() method creates and returns a new string which contains a specified number of copies of a passed string, concatenated together.

A number is passed as an argument in the method as the number of times string should be repeated. The number must be between 0 and +Infinity. Float value will be converted to Integer when passed.

Example

Try It

11. replace in javascript

The replace() method returns a new string after replacing some or all occurrences of some pattern with another replacement in the given string.

The pattern can be a string or regex, and the replacement can be a string to be replaced or function to be called for each match.

When the pattern is string only first occurrence of it will be replaced.

Example

Try It

12. replaceAll in javascript

The replaceAll() method returns a new string after replacing all occurrences of some pattern with another replacement in the given string.

The pattern can be a string or regex, and the replacement can be a string to be replaced or function to be called for each match.

When using regex in replaceAll() method using a global flag with it otherwise it will throw a 'TypeError'.

Example

Try It

13. search in javascript

The search() string method in javascript uses regex to search for a pattern in a string, if a pattern exists in the string then it returns the index of the first occurrence of pattern.

Example

Try It

14. slice in javascript

The slice() string method in javascript extracts a part of the string and returns as a new string.

str.slice( startIndex, endIndex)

The slice() takes 2 argument, the first argument is start index for slicing string and second is the end of the slicing string, where the second argument is optional.

When the second argument is not passed then the string is sliced from 'startIndex' to the end of the string.

The slice() method also accepts negative value, where -1 represents last index

Example

Try It

15. split in javascript

The split() string method in javascript divides a string into a list of substrings based on the split value and returns this element by putting it in an array.

The split() method accepts 2 argument, first is separator: it can be a string or regex which describe where each split should occur (it is optional), second is limit: it is a number which limits the number of elements in the returning array (it is also optional).

Example

Try It

16. startsWith in javascript

The startsWith() string method in javascript determines whether a string starts with some string or not. If it starts with the desired string then it returns true else return false.

The search string is passed as an argument. There is also an optional argument which defines the position where the method should start checking.

Example

Try It

17. substr in javascript

The substr() string method in javascript returns a part of the string, starting at a specific index and ending after a given number of characters.

Example

Try It

18. toLowerCase in javascript

The toLowerCase() string method in javascript returns a new string by converting the calling string to lowercase.

Example

Try It

19. toUpperCase in javascript

The toUpperCase() string method in javascript returns a new string by converting the calling string to uppercase.

Example

Try It

20. toString in javascript

The toString() string method in javascript returns a string representing the specified object.

This method also convert numbers to strings in a different number system. You can pass 2 to convert any decimal number to binary number.

Example

Try It

21. trim in javascript

The trim() string method in javascript removes whitespaces from both ends of the string. Whitespaces are space, tabs, newline, etc.

Example

Try It

22. valueOf in javascript

The valueOf() string method in javascript returns the primitive value of a String object.

Example

Try It