Learn everything about JavaScript String Methods with code examples. This guide is the ultimate cheat sheet of Javascript string methods.

JavaScript String Methods Complete Guide
JavaScript String Methods Complete Guide

Strings are fundamental data types in JavaScript that represent text. In JavaScript, strings are objects that have their own set of methods.

These methods can be used to manipulate strings in various ways, such as finding the length of a string, converting it to uppercase or lowercase, replacing characters, splitting it into an array, and more.

In this article, we will explore some of the most commonly used JavaScript string methods with code examples.

By the end of this article, you will have a better understanding of how to work with strings in JavaScript.

1. charAt()

The charAt() method returns the character at a specified index in a string. It takes the index of the character to return and returns an empty string if the index is out of range.

const str = "hello";
const char = str.charAt(1);
console.log(char); // "e"

2. charCodeAt()

The charCodeAt() method returns the Unicode value of the character at a specified index in a string. It takes the index of the character to return and returns NaN if the index is out of range.

const str = "hello";
const code = str.charCodeAt(1);
console.log(code); // 101 (Unicode value for "e")

3. concat()

The concat() method concatenates two or more strings into a single string. It takes any number of strings as arguments and returns the concatenated string.

const str1 = "hello";
const str2 = "world";
const str3 = str1.concat(" ", str2);
console.log(str3); // "hello world"

4. endsWith()

The endsWith() method tests whether a string ends with a specified substring. It takes the substring to search for and an optional length parameter that specifies how many characters from the end of the string to search. It returns true if the string ends with the substring, and false otherwise.

const str = "hello world";
const endsWith = str.endsWith("world");
console.log(endsWith); // true

5. includes()

The includes() method tests whether a string contains a specified substring. It takes the substring to search for and an optional starting index to begin the search. It returns true if the string contains the substring, and false otherwise.

const str = "hello world";
const includes = str.includes("world");
console.log(includes); // true

6. indexOf()

The indexOf() method returns the index of the first occurrence of a specified substring in a string. It takes the substring to search for and an optional starting index to begin the search. It returns the index of the first occurrence of the substring, or -1 if the substring is not found.

const str = "hello world";
const index = str.indexOf("world");
console.log(index); // 6

7. lastIndexOf()

The lastIndexOf() method returns the index of the last occurrence of a specified substring in a string. It takes the substring to search for and an optional starting index to begin the search. It returns the index of the last occurrence of the substring, or -1 if the substring is not found.

const str = "hello world";
const lastIndex = str.lastIndexOf("o");
console.log(lastIndex); // 7

8. match()

The match() method searches a string for a specified pattern and returns an array of matches. It takes a regular expression or a string to search for and returns an array of matches or null if no matches are found.

const str = "The quick brown fox";
const matches = str.match(/o/g);
console.log(matches); // ["o", "o"]

9. repeat()

The repeat() method returns a new string that repeats a specified string a specified number of times. It takes the number of times to repeat the string and returns the repeated string.

const str = "hello";
const repeated = str.repeat(3);
console.log(repeated

10. slice()

The slice() method returns a section of a string between two specified indexes. It takes the starting and ending indexes and returns the section of the string between them. If no end index is specified, the method returns the rest of the string.

const str = "hello world";
const section = str.slice(6, 11);
console.log(section); // "world"

11. split()

The split() method splits a string into an array of substrings based on a specified separator. It takes the separator to use and an optional limit on the number of splits. It returns an array of substrings.

const str = "hello,world";
const parts = str.split(",");
console.log(parts); // ["hello", "world"]

3. startsWith()

The startsWith() method tests whether a string starts with a specified substring. It takes the substring to search for and an optional starting index to begin the search. It returns true if the string starts with the substring, and false otherwise.

const str = "hello world";
const startsWith = str.startsWith("hello");
console.log(startsWith); // true

4. substring()

The substring() method returns a section of a string between two specified indexes. It takes the starting and ending indexes and returns the section of the string between them. If no end index is specified, the method returns the rest of the string

const str = "hello world";
const section = str.substring(6, 11);
console.log(section); // "world"

5. toLowerCase()

The toLowerCase() method returns a new string with all the characters converted to lowercase. It takes no parameters and returns the lowercase string.

const str = "HELLO WORLD";
const lowercase = str.toLowerCase();
console.log(lowercase); // "hello world"

10. replace()

The replace() method searches a string for a specified substring or regular expression and replaces the first occurrence with a specified replacement string. It takes the substring or regular expression to search for and the replacement string. It returns a new string with the first occurrence of the substring or regular expression replaced.

const str = "hello world";
const replaced = str.replace("world", "JavaScript");
console.log(replaced); // "hello JavaScript"

The search() method searches a string for a specified substring or regular expression and returns the index of the first occurrence. It takes the substring or regular expression to search for and returns the index of the first occurrence or -1 if the substring or regular expression is not found.

const str = "hello world";
const index = str.search("world");
console.log(index); // 6

12. slice()

The slice() method extracts a section of a string and returns a new string. It takes the starting and ending index of the section to extract and returns the extracted section

const str = "hello world";
const section = str.slice(6, 11);
console.log(section); // "world"

13. split()

The split() method splits a string into an array of substrings based on a specified separator. It takes the separator string or regular expression and an optional limit parameter that specifies the maximum number of splits. It returns an array of substrings.

const str = "hello world";
const arr = str.split(" ");
console.log(arr); // ["hello", "world"]

14. startsWith()

The startsWith() method tests whether a string starts with a specified substring. It takes the substring to search for and an optional starting index to begin the search. It returns true if the string starts with the substring, and false otherwise.

const str = "hello world";
const startsWith = str.startsWith("hello");
console.log(startsWith); // true

15. substring()

The substring() method extracts a section of a string and returns a new string. It takes the starting and ending index of the section to extract and returns the extracted section.

const str = "hello world";
const section = str.substring(6, 11);
console.log(section); // "world"

16. toLowerCase()

The toLowerCase() method returns a new string with all the characters of a string converted to lowercase.

const str = "HELLO WORLD";
const lowerCase = str.toLowerCase();
console.log(lowerCase); // "hello world"

17. toUpperCase()

The toUpperCase() method returns a new string with all the characters of a string converted to uppercase.

const str = "hello world";
const upperCase = str.toUpperCase();
console.log(upperCase); // "HELLO WORLD"

18. trim()

The trim() method removes whitespace from both ends of a string and returns a new string.

const str = "    hello world    ";
const trimmed = str.trim();
console.log(trimmed); // "hello world"