JavaScript String Cheatsheet: Everything You Need to Know

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');
MethodsCode ExampleResultReturn Type
indexOfstr.indexOf('L')2number
indexOf()'hello'.indexOf('l')2number
indexOf()str.indexOf('L')2number
lastIndexOf()'hello'.lastIndexOf('l')3number
search()str.search(/[a-z]/g)1number
endsWith()str.endsWith('z')FALSEboolean
endsWith()str.endsWith('o', 6)TRUEboolean
includes()str.includes('a')FALSEboolean
startsWith()str.startsWith('H', 1)FALSEboolean
startsWith()str.startsWith('H')TRUEboolean
charAt()str.charAt(1)estring
concat()str.concat(' grace')HeLlo gracestring
padEnd()str.padEnd(10, '!')HeLlo!!!!!string
padStart()str.padStart(10, '!')!!!!!HeLlostring
repeat()str.repeat(3))HeLloHeLloHeLlostring
replace()str.replace('L', 'l')Hellostring
replaceAll()hello'.replaceAll('l', 'z')hezzostring
slice()str.slice(1, 3)eLstring
slice()str.slice(2)Llostring
substring()str.substring(2)Llostring
substring()str.substring(1, 3)eLstring
toLowerCase()str.toLowerCase()hellostring
toUpperCase()str.toUpperCase()HELLOstring
trim()' hello '.trim()'hello'string
trimEnd()' hello '.trimEnd()' hello'string
trimStart()' hello '.trimStart()'hello 'string
valueOf()stringObj.valueOf()gracestring
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

String.charAt()

Returns a string representing the character at the given index.

const str = "Hello World";
str.charAt(0); // "H"

String.charCodeAt()

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

String.concat()

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"

String.endsWith()

Returns true if the string ends with the given string, otherwise false.

const str = "Hello World";
str.endsWith("World"); // true

String.includes()

Returns true if the string contains the given string, otherwise false.

const str = "Hello World";
str.includes("World"); // true

String.indexOf()

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

String.lastIndexOf()

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

String.match()

Returns a list of matches of a regular expression against a string.

const str = "Hello World";
str.match(/[A-Z]/); // ["H"]

String.matchAll()

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"]

String.padEnd()

Returns a new string with some content padded to the end of the string.

const str = "Hello";
str.padEnd(15, "World"); // "HelloWorldWorld"

String.padStart()

Returns a new string with some content padded to the start of the string.

const str = "Hello";
str.padStart(15, "World"); // "WorldWorldWorldHello"

String.repeat()

Returns a new string which contains the specified number of copies of the string.

const str = "Hello";
str.repeat(3); // "HelloHelloHello"

String.replace()

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"

String.replaceAll()

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"

String.search()

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

String.slice()

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"

String.split()

Returns an array of strings split at the given index.

const str = "Hello World";
str.split(" "); // ["Hello", "World"]

String.startsWith()

Returns true if the string starts with the given string, otherwise false.

const str = "Hello World";
str.startsWith("Hello"); // true

String.substring()

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"

NOTE: substring takes parameters as (from, to).

String.substr()

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).

String.toLowerCase()

Returns a new string with all the uppercase characters converted to lowercase.

const str = "Hello World";
str.toLowerCase(); // "hello world"

String.toUpperCase()

Returns a new string with all the lowercase characters converted to uppercase.

const str = "Hello World";
str.toUpperCase(); // "HELLO WORLD"

String.toString()

Returns the string representation of the specified object.

const str = new String("Hello World");
console.log(str); // Object of String
str.toString(); // "Hello World"

String.trim()

Returns a new string with the leading and trailing whitespace removed.

const str = "  Hello World  ";
str.trim(); // "Hello World"

String.trimEnd()

Returns a new string with the trailing whitespace removed.

const str = "  Hello World  ";
str.trimEnd(); // "  Hello World"

String.trimStart()

Returns a new string with the leading whitespace removed.

const str = "  Hello World  ";
str.trimStart(); // "Hello World  "

Thank you for reading 😊

#javascript #js 

JavaScript String Cheatsheet: Everything You Need to Know
22.25 GEEK