If you were looking for a handy JavaScript cheatsheet for all the string methods, including their parameters, descriptions, and examples, you've found it.
Note you can use the table of contents above or CMD + F to quickly find what you're looking for.
charAt()
Parameters: index
(optional)
Description: Returns the character at the specified index in a string.
Example:
let str = "Hello";
console.log(str.charAt(1)); // Output: "e"
charCodeAt()
Parameters: index
(optional)
Description: Returns the Unicode of the character at the specified index in a string.
Example:
let str = "Hello";
console.log(str.charCodeAt(1)); // Output: 101
concat()
Parameters: string2, string3, ..., stringX
Description: Joins two or more strings.
Example:
let str1 = "Hello";
let str2 = "World";
console.log(str1.concat(" ", str2)); // Output: "Hello World"
endsWith()
Parameters: searchvalue, length
(optional)
Description: Checks whether a string ends with specified characters.
Example:
let str = "Hello World";
console.log(str.endsWith("World")); // Output: true
includes()
Parameters: searchvalue, start
(optional)
Description: Checks whether a string contains the specified string/characters.
Example:
let str = "Hello World";
console.log(str.includes("World")); // Output: true
indexOf()
Parameters: searchvalue, start
(optional)
Description: Returns the position of the first occurrence of a specified value in a string.
Example:
let str = "Hello World";
console.log(str.indexOf("o")); // Output: 4
lastIndexOf()
Parameters: searchvalue, start
(optional)
Description: Returns the position of the last occurrence of a specified value in a string.
Example:
let str = "Hello World";
console.log(str.lastIndexOf("o")); // Output: 7
match()
Parameters: regexp
Description: Searches a string for a match against a regular expression, and returns the matches.
Example:
let str = "The rain in SPAIN stays mainly in the plain";
console.log(str.match(/ain/gi)); // Output: ["ain", "AIN", "ain", "ain"]
padEnd()
Parameters: targetLength, padString
(optional)
Description: Pads the current string with a given string (repeated, if needed) so that the resulting string reaches a given length.
Example:
let str = "5";
console.log(str.padEnd(4, "0")); // Output: "5000"
padStart()
Parameters: targetLength, padString
(optional)
Description: Pads the current string with another string (multiple times, if needed) until the resulting string reaches the given length.
Example:
let str = "5";
console.log(str.padStart(4, "0")); // Output: "0005"
repeat()
Parameters: count
Description: Returns a new string with a specified number of copies of the string it was called on.
Example:
let str = "Hello";
console.log(str.repeat(3)); // Output: "HelloHelloHello"
replace()
Parameters: searchFor, replaceWith
Description: Searches a string for a specified value, or a regular expression, and returns a new string where the specified values are replaced.
Example:
let str = "Hello World";
console.log(str.replace("World", "Universe")); // Output: "Hello Universe"
replaceAll()
Parameters: searchFor, replaceWith
Description: Returns a new string with all matches of a pattern replaced by a replacement.
Example:
let str = "cat cat cat";
console.log(str.replaceAll("cat", "dog")); // Output: "dog dog dog"
search()
Parameters: regexp
Description: Searches a string for a specified value, or regular expression, and returns the position of the match.
Example:
let str = "Hello World";
console.log(str.search("World")); // Output: 6
slice()
Parameters: start, end
(optional)
Description: Extracts a part of a string and returns a new string.
Example:
let str = "Hello World";
console.log(str.slice(6)); // Output: "World"
split()
Parameters: separator, limit
(optional)
Description: Splits a string into an array of substrings.
Example:
let str = "How are you doing today?";
console.log(str.split(" ")); // Output: ["How", "are", "you", "doing", "today?"]
startsWith()
Parameters: searchvalue, start
(optional)
Description: Checks whether a string begins with specified characters.
Example:
let str = "Hello World";
console.log(str.startsWith("Hello")); // Output: true
substring()
Parameters: start, end
(optional)
Description: Extracts the characters from a string, between two specified indices.
Example:
let str = "Hello World";
console.log(str.substring(6, 11)); // Output: "World"
toLowerCase()
Parameters: None
Description: Converts a string to lowercase letters.
Example:
let str = "Hello World";
console.log(str.toLowerCase()); // Output: "hello world"
toUpperCase()
Parameters: None
Description: Converts a string to uppercase letters.
Example:
let str = "Hello World";
console.log(str.toUpperCase()); // Output: "HELLO WORLD"
trim()
Parameters: None
Description: Removes whitespace from both sides of a string.
Example:
let str = " Hello World ";
console.log(str.trim()); // Output: "Hello World"
trimStart()
Parameters: None
Description: Removes whitespace from the beginning of a string.
Example:
let str = " Hello World ";
console.log(str.trimStart()); // Output: "Hello World "
trimEnd()
Parameters: None
Description: Removes whitespace from the end of a string.
Example:
let str = " Hello World ";
console.log(str.trimEnd()); // Output: " Hello World"
Final words
I ope this was helpful and you can bookmark this for future reference! More JavaScript cheatsheets on the way, make sure to subscribe for the weekly newsletter below to stay up to date on the latest articles we release to level up your skills.