Master JavaScript strings with this comprehensive cheatsheet! This comprehensive JavaScript string cheatsheet is the perfect resource for anyone who wants to learn everything there is to know about strings in JavaScript. Covering a wide range of topics, from basic string operations to advanced string methods, this cheatsheet has everything you need to start using strings effectively in your code.
📘 15+ Best JavaScript Books for Beginners and Experienced Coders
const str = 'HeLlo';
const stringObj = new String('grace');
Methods | Code Example | Result | Return Type |
---|---|---|---|
indexOf | str.indexOf('L') | 2 | number |
indexOf() | 'hello'.indexOf('l') | 2 | number |
indexOf() | str.indexOf('L') | 2 | number |
lastIndexOf() | 'hello'.lastIndexOf('l') | 3 | number |
search() | str.search(/[a-z]/g) | 1 | number |
endsWith() | str.endsWith('z') | FALSE | boolean |
endsWith() | str.endsWith('o', 6) | TRUE | boolean |
includes() | str.includes('a') | FALSE | boolean |
startsWith() | str.startsWith('H', 1) | FALSE | boolean |
startsWith() | str.startsWith('H') | TRUE | boolean |
charAt() | str.charAt(1) | e | string |
concat() | str.concat(' grace') | HeLlo grace | string |
padEnd() | str.padEnd(10, '!') | HeLlo!!!!! | string |
padStart() | str.padStart(10, '!') | !!!!!HeLlo | string |
repeat() | str.repeat(3)) | HeLloHeLloHeLlo | string |
replace() | str.replace('L', 'l') | Hello | string |
replaceAll() | hello'.replaceAll('l', 'z') | hezzo | string |
slice() | str.slice(1, 3) | eL | string |
slice() | str.slice(2) | Llo | string |
substring() | str.substring(2) | Llo | string |
substring() | str.substring(1, 3) | eL | string |
toLowerCase() | str.toLowerCase() | hello | string |
toUpperCase() | str.toUpperCase() | HELLO | string |
trim() | ' hello '.trim() | 'hello' | string |
trimEnd() | ' hello '.trimEnd() | ' hello' | string |
trimStart() | ' hello '.trimStart() | 'hello ' | string |
valueOf() | stringObj.valueOf() | grace | string |
match() | str.match(/[A-Z]/g) | ["H", "L"] | array |
split() | str.split() | ["HeLlo"] | array |
split() | str.split('') | ["H", "e", "L", "l", "o"] | array |
Javascript String Cheat Sheet
const text = "Hey, Devs!";
text.charAt(1); // Returns character at a specified position
text.charCodeAt(1); // Returns the Unicode of a character at a specified position
text.fromCharCode(89, 43, 90, 61); // Returns a string from the specific sequence of UTF-16 units
text.indexOf('H'); // Returns the position of the first occurrence of the specified text
text.lastIndexOf('e'); // Returns the position of the last occurrence of the specified text
text.search(regex); // Executes a search for a matching text and returns its position
text.match(regex); // Retrieves the matches of a string and returns it as a new string
text.concat("!"); // Concatenates two or more strings into one
text.replace('Hey', 'Yo'); // Find and replace specified text in a string
text.slice(1,3); // Extracts a section of a string and returns it as a new string
text.split(','); // Splits a string in an array of strings based on a given character
text.substr(1,2); // Extracts a substring in a specified range of characters
text.substring(1,2); // Similar to slice() but can't accept negative indices
text.toLowerCase(); // Converts strings to lowercase
text.toUpperCase(); // Converts strings to uppercase
text.valueOf(); // Returns the primative value of a string object
Returns a string representing the character at the given index.
const str = "Hello World";
str.charAt(0); // "H"
Returns a number representing the UTF-16 code unit value of the character at the given index.
const str = "Hello World";
str.charCodeAt(0); // 72
Returns a new string containing the concatenation of the given strings.
const str = "Hello";
const str2 = " World";
str.concat(str2); // "Hello World"
console.log(`${str}${str2}`); // "Hello World"
console.log(str + str2); // "Hello World"
Returns true if the string ends with the given string, otherwise false.
const str = "Hello World";
str.endsWith("World"); // true
Returns true if the string contains the given string, otherwise false.
const str = "Hello World";
str.includes("World"); // true
Returns the index within the string of the first occurrence of the specified value, or -1 if not found.
const str = "Hello World";
str.indexOf("World"); // 6
Returns the index within the string of the last occurrence of the specified value, or -1 if not found.
const str = "Hello World";
str.lastIndexOf("World"); // 6
Returns a list of matches of a regular expression against a string.
const str = "Hello World";
str.match(/[A-Z]/); // ["H"]
Returns a list of matches of a regular expression against a string.
const str = "Hello World";
str.matchAll(/[A-Z]/g); // ["H", "W"]
// OR
str.match(/[A-Z]/g); // ["H", "W"]
Returns a new string with some content padded to the end of the string.
const str = "Hello";
str.padEnd(15, "World"); // "HelloWorldWorld"
Returns a new string with some content padded to the start of the string.
const str = "Hello";
str.padStart(15, "World"); // "WorldWorldWorldHello"
Returns a new string which contains the specified number of copies of the string.
const str = "Hello";
str.repeat(3); // "HelloHelloHello"
Returns a new string with some or all matches of a regular expression replaced by a replacement string.
const str = "Hello World";
str.replace("l", "*"); // "He*lo World"
Returns a new string with some or all matches of a regular expression replaced by a replacement string.
const str = "Hello World";
str.replaceAll("l", "*"); // "He**o Wor*d"
OR;
str.replace(/l/g, "*"); // "He**o Wor*d"
Returns the index within the string of the first occurrence of the specified value, or -1 if not found.
const str = "Hello World 1";
const regex = /[^\D\s]/g; // Find digit
str.search(regex); // 12
Returns a new string containing the characters of the string from the given index to the end of the string.
const str = "Hello World";
str.slice(6); // "World"
Returns an array of strings split at the given index.
const str = "Hello World";
str.split(" "); // ["Hello", "World"]
Returns true if the string starts with the given string, otherwise false.
const str = "Hello World";
str.startsWith("Hello"); // true
Returns a new string containing the characters of the string from the given index to the end of the string.
const str = "Hello World";
str.substring(1, 2); // "e"
Returns a new string containing the characters of the string from the given index to the end of the string.
const str = "Hello World";
str.substr(1, 2); // "el"
NOTE: substr takes parameters as (from, length).
Returns a new string with all the uppercase characters converted to lowercase.
const str = "Hello World";
str.toLowerCase(); // "hello world"
Returns a new string with all the lowercase characters converted to uppercase.
const str = "Hello World";
str.toUpperCase(); // "HELLO WORLD"
Returns the string representation of the specified object.
const str = new String("Hello World");
console.log(str); // Object of String
str.toString(); // "Hello World"
Returns a new string with the leading and trailing whitespace removed.
const str = " Hello World ";
str.trim(); // "Hello World"
Returns a new string with the trailing whitespace removed.
const str = " Hello World ";
str.trimEnd(); // " Hello World"
Returns a new string with the leading whitespace removed.
const str = " Hello World ";
str.trimStart(); // "Hello World "
Thank you for reading 😊
#javascript #js