JavaScript String Functions
Basic String Methods
Function Description
length Returns the length of the string.
charAt(index) Returns the character at the specified index.
charCodeAt(index) Returns the Unicode value of the character at the specified index.
concat(str1, str2, ...) Joins two or more strings.
Searching
Function Description
includes(substring) Checks if the string contains the specified substring.
indexOf(substring) Returns the index of the first occurrence (or -1 if not found).
lastIndexOf(substring) Returns the index of the last occurrence.
startsWith(substring) Checks if the string starts with the given substring.
endsWith(substring) Checks if the string ends with the given substring.
Trimming and Modifying
Function Description
trim() Removes whitespace from both ends.
trimStart() / trimEnd() Removes whitespace from the start or end.
toUpperCase() Converts to uppercase.
toLowerCase() Converts to lowercase.
replace(search, replacement) Replaces a substring or pattern with something else.
replaceAll(search, replacement)Replaces all occurrences of a substring. (ES2021+)
Slicing and Splitting
Function Description
slice(start, end) Extracts a section of a string.
substring(start, end) Similar to slice but doesn't accept negative indexes.
substr(start, length) Extracts a substring of a given length (deprecated).
split(separator) Splits a string into an array based on a delimiter.
Testing and Matching
Function Description
match(regex) Matches a string against a regular expression.
matchAll(regex) Returns all matches with capturing groups. (ES2020+)
search(regex) Searches for a match using a regex and returns the index.
Other Useful Methods
Function Description
repeat(n) Repeats the string n times.
padStart(length, padString) Pads the start of a string.
padEnd(length, padString) Pads the end of a string.
localeCompare(otherString) Compares two strings based on locale.