Tutorials Tonight's Logo
Follow Us

JAVASCRIPT STRING METHODS LIST


What Are String Methods In Javascript?

Javascript String methods are special build-in functions that play out a particular assignment when applied on any valid string. These methods return a new string dependent on the method applied without changing the first string.

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

javascript string method

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

Test String = "Hello World"

Using indexOf() method


Javascript String Length

Before discussing string methods let's look at 1 string property that javascript has which is the length property.

The length is a string property that is used to find the length of a string. It returns a number of code units in the string including spaces as an integer.

The length is only a read-only data property of a string.

The length of an empty string is 0.

Example

const str = "Hello World";
const lengthOfString = str.length; // output: 11
console.log("lengthOfString = " + lengthOfString);
const empty = "";
console.log(empty.length);
▶ Try It

Javascript String Methods List

In JavaScript, there are a large number of string methods. We have listed some of the most frequently use string methods for you. 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 the index value. An index value of a string starts with 0, which means the first character has an index value of 0, the second character has an index value of 1, and so on.

The default index value in the charAt method is 0, if the index value is out of range or can not be converted to an integer then this method returns the first character of the string.

Example

const str = "Hello World";
console.log(str.charAt()); //default index value = 0, return first character
console.log(str.charAt(1));
console.log(str.charAt(13)); // index out of range, return empty string
console.log(str.charAt(6));
▶ Try It

Alternatively, you can consider the string as a character of the array and call out any character of the string by its index value, as all the characters are a member of the array.

Example

const str = "Hello World";
console.log(str[0]);
console.log(str[1]);
console.log(str[13]); // index out of range, return empty string

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. Example Unicode value of 'A' is 65.

The Unicode value lies between 0 and 65535 which represent the UTF-16 code unit of character at the specified index.

The charCodeAt() method takes an argument which is index value of the string and returns the Unicode value of the character present at that index value.

The default value of the index is 0. If the index value is not given or it can't be converted to a string the 0 is used by default.

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

Example

const str = "Hello World";
console.log(str.charCodeAt(1));
console.log(str.charCodeAt()); //default index value = 0
console.log(str.charCodeAt("x")); //default index value = 0
console.log(str.charCodeAt(13)); // return NaN
▶ Try It

3. concat in javascript

The concat string method in javascript concatenates the passed string in the method to the calling string and return the concatenated string as 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 the argument to string and then concatenates it.

Example

const firstName = "Hello";
const lastname = "World"
console.log(firstName.concat(" ", lastname));
// multiple arguments
console.log("Learning".concat(" ", "to", " ", "code."));
▶ Try It

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

Example

const firstName = "Hello";
const lastname = "World"
console.log(firstName + " " + lastname);
console.log("Learning" + " " +  "to" + " " + "code.");

4. String includes in javascript

The includes string method in javascript is used determines whether a given substring is present in the calling string or not.

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

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

Example

const sentence = "Carbon emittion is increasing Day by day.";
const word1 = "day";
const word2 = "Day";
const word3 = "DAY";
console.log(sentence.includes(word1));
console.log(sentence.includes(word2));
console.log(sentence.includes(word3));
▶ Try It

The includes also accept a second argument which is optional. It determines the position from where the method starts searching within the string

Example

const sentence = "Carbon emittion is increasing day by day.";
console.log(sentence.includes("is", 20));
console.log(sentence.includes("is", 2));
console.log(sentence.includes("emittion", 5));

5. endsWith in javascript

The endsWith string method in javascript is used to determines whether the string ends with a specified substring or not.

If it ends with a specified string then it returns true, else returns false.

The substring to be checked for the ending is passed as an argument in the method.

There is a second argument in the method which is optional. It specifies a length value for the string.

Please note that endsWith() method is case sensitive.

Example

const question = "What is DOM?";
const endString = "?";
console.log(question.endsWith(endString));
console.log(question.endsWith(endString, 10));
▶ Try It

6. indexOf in JavaScript

The indexOf string method in javascript is used to determine whether a character or substring is present in a string.

If the 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. The search string is case-sensitive.

If no search string is provided to the method then default search string is 'undefined'. Example 'undefined'.indexOf() will return 0.

There is a second argument that is optional that defines the start index from where the method starts to search for the character or substring.

Example

const sentence = "Carbon emittion is increasing day by day";
const char = "day";
console.log(sentence.indexOf(char));
console.log(sentence.indexOf("Carbon"));
console.log(sentence.indexOf("carbon")); // case-sensitive
console.log(sentence.indexOf(char, 40));
▶ Try It

7. String lastIndexOf JavaScript

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

If a 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.

lastIndexOf string method

If no character or substring is passed then the default search is for 'undefined'. Example 'undefined'.lastIndexOf() will return 0, because there is no argument and now the lastIndexOf method will search for undefined.

There is an optional argument that is fromIndex. Its default value is +Infinity. If fromIndex >= str.length then whole string searched. See lastIndexOf string method in detail.

Example

const sentence = "to do or not to do";
const char = "to";
console.log(sentence.lastIndexOf(char));
console.log(sentence.lastIndexOf(char, 10));
▶ Try It

8. match in javascript

The match() string method in javascript uses a regular expression to match a series of characters (substring) within the calling string.

The method returns the output as an array string with matched character or strings as its element.

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

Example

const series = "bdWg2AdjgH4du5jUgT";
const regex = /[A-Z0-9]/g;
console.log(series.match(regex));
▶ Try It

If the used regular expression does not have a g flag then the method returns only the first complete match and its capturing group.

If nothing matches within the string then the method returns null.

Example

const string = "to do or not to do";
let match1 = string.match(/do/);
let match2 = string.match(/dog/);
console.log(match1);
console.log(match2);
▶ Try It

9. matchAll in javascript

The matchAll() string method in javascript is an improved variant of the match() method. In the match method when the regular expression is used without the g flag then it works fine but with the g flag, it only returns the matches and not its capturing group.

But the matchAll() method finds all the matching string with its capturing group against the regular expression.

Instead of returning an array the matchAll() method returns an iterator to all results matching the string. To convert it to string you can use Array.from() method.

Example

const series = "to do or not to do";
const regex = /d(o)/g;
const array = Array.from(series.matchAll(regex));
console.log(array);
console.log(array[0])
▶ Try It

10. repeat in javascript

The repeat() method creates and returns a new string by concatenating a passed string by a specified number of times.

A number of times string is to be repeated is passed as an argument in the method, where the number lies between 0 and +Infinity.

If a decimal value is passed then it will be converted to an Integer when passed.

The method reports an error for a negative number.

repeat method negative number error

Example

const str = "Tick tock, ";
console.log(str.repeat(0));
console.log(str.repeat(3.5));
console.log(str.repeat(2));
▶ Try It

11. replace in javascript

The replace method matches a part of a string by using a substring or a regular expression as a pattern and replace it with a replacement string.

After replacing some or all occurrences of pattern with another replacement string method returns the replaced string as a new string. The original string is unmodified.

The pattern can be a string or regular expression, if the pattern is a string then the method replaces only the first match but if the pattern is a regular expression then the method replaces all the matched by the replacement string.

Further, the replacement can be a string to be replaced or a function to be called for each match.

Example

const str = "Carbon emittion is increasing day by day.";
console.log(str.replace(/day/g, "year")); // all replace
console.log(str.replace("day", "year")); // only first occurance replace
▶ Try It

12. replaceAll in javascript

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

Unlike replace() method it replaces all the occurrences whether the given pattern is a string or a regular expression.

The replacement can be a string to be replaced or a function to be called for each match.

When using regular expression in the replaceAll() method using a global flag (g) is compulsory with it otherwise it will throw a 'TypeError'.

Example

const str = "Carbon emittion is increasing day by day.";
console.log(str.replaceAll("day", "year"));
console.log(str.replaceAll(/day/g, "month"));
▶ Try It

13. search in javascript

The search string method in javascript is used to determine whether a pattern exists within the calling string or not, if it exists then the method returns the index value of the first match within the string.

The search method uses regex to search for a pattern in a string, if a string is passed to search for then the method implicitly convert it to regex.

Example

const str = "kjhBfdbAjdbj";
console.log(str.search(/[A-Z]/g));
▶ Try It

14. slice in javascript

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

javascript slice method
str.slice( startIndex, [, endIndex])

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

The default value of endIndex is str.length 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 the last index

Example

const sentence = "Carbon emittion is increasing day by day";
console.log(sentence.slice(5, 15));
console.log(sentence.slice(5));
console.log(sentence.slice(-15, -5));
▶ Try It

15. split in javascript

The split string method in javascript cuts a string into a list of substrings based on the split value and returns an array by putting these substrings in the array.

The split() method accepts 2 arguments, 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 an integer that limits the number of elements in the returning array (it is also optional).

If the separator is an empty string ('') then the method split the string at each character.

Example

const sentence = "Carbon emittion is increasing day by day";
console.log(sentence.split());
console.log(sentence.split(" "));
console.log(sentence.split("is"));
console.log(sentence.split("")); // split at each character
▶ Try It

16. startsWith in javascript

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

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

The startsWith method is case-sensitive.

Example

const sentence = "Carbon emittion is increasing day by day";
console.log(sentence.startsWith("Car"));
console.log(sentence.startsWith("carbon")); // return false case-sensitive
console.log(sentence.startsWith("bon", 3));
▶ Try It

17. substr in javascript

The substr string method in javascript is used to extract a substring from a string. It returns a part of the string, starting at a specific index and ending after a given number of characters.

The first argument of the method is startIndex which specifies the index value from where the substring starts. The second argument is the length of the substring which is also optional. If the default value of the second argument is str.length.

If the value of startIndex is negative then the index is counted from the end of the string in opposite direction. If it is NaN then it is treated as 0.

Example

const sentence = "Carbon emittion is increasing day by day";
console.log(sentence.substr(10, 6));
console.log(sentence.substr(10));
▶ Try It

18. toLowerCase in javascript

The toLowerCase() string method in javascript converts the case of a string to lowercase and returns it as a new string.

Example

const sentence = "CARBON EMITTION IS INCREASING DAY BY DAY";
console.log(sentence.toLowerCase());
▶ Try It

19. toUpperCase in javascript

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

Example

const sentence = "carbon emittion is increasing day by day";
console.log(sentence.toUpperCase());
▶ 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 a binary number.

Example

const str = new String("hello World!");
console.log(str.toString(str));
const num = 20;
console.log(num.toString(2));
▶ 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

const str = "    TutorialsTonight    ";
console.log(str.trim());
▶ Try It

22. valueOf in javascript

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

Example

const str = new String("hello world");
console.log(str.valueOf(str));
▶ Try It

Conclusion

You have seen 22 different string methods in javascript with their use and examples. This string method makes your job easy while dealing with string in javascript.

More Learning

Want to learn more about programming and web development? We have a few recommendations for you.


Follow Us

Copyright © Tutorials Tonight. All Rights Are Reserved.