Cheat Sheet of JavaScript String Methods
These are built-in functions used to manipulate and process strings. Below is an explanation of each method:
1. Splitting and Slicing Strings
str.split(separator, limit): Splits a string into an array based on a separator.
str.slice(start, end): Cuts a section of a string and returns it as a new string.
2. Extracting Parts of a String
str.substring(start, end): Similar to slice(), but cannot accept negative indexes.
str.substr(start, length): Cuts the string based on a start index and a length.
3. Changing Case
str.toUpperCase(): Converts all characters to uppercase.
str.toLowerCase(): Converts all characters to lowercase.
4. Checking Start and End of a String
str.startsWith(value, position): Checks if a string starts with a given value.
str.endsWith(value, position): Checks if a string ends with a given value.
5. Searching for Content
str.search(regExp): Searches for a match using a regular expression and returns the index.
str.match(regExp): Returns an array of matches found using a regular expression.
str.matchAll(regExp): Returns an iterator of all matches using a regular expression.
6. Finding Position of Substrings
str.indexOf(value, position): Returns the index of the first occurrence of a value.
Get Jakariya H.’s stories in your inbox
Join Medium for free to get updates from this writer.
str.lastIndexOf(value, position): Returns the index of the last occurrence of a value.
7. Trimming Whitespace
str.trim(): Removes whitespace from both ends of the string.
str.trimStart(): Removes whitespace from the beginning.
str.trimEnd(): Removes whitespace from the end.
8. Padding Strings
str.padStart(length, string): Pads the string at the start to reach the specified length.
str.padEnd(length, string): Pads the string at the end to reach the specified length.
9. Character and ASCII Code Operations
str.charAt(index): Returns the character at the specified index.
str.charCodeAt(index): Returns the Unicode value of the character at a given index.
str.fromCharCode(n1, n2, ...): Converts Unicode values to characters.
10. Combining and Repeating Strings
str.concat(str1, str2, ...): Joins multiple strings together.
str.includes(value, position): Checks if a string contains a specific value.
str.repeat(count): Repeats a string count times.
11. Replacing Content
str.replace(str|regExp, newStr|Fn): Replaces the first occurrence of a substring or regex match.
str.replaceAll(str|regExp, newStr): Replaces all occurrences of a substring or regex match.
12. Converting to Other Formats
str.toString(): Converts a string object to a string.
str.at(index): Returns the character at a given index.
str.valueOf(): Returns the primitive string value of a String object.
str.length (Property): Returns the length of the string.








