let name = "John Doe"; // "John Doe"
\ - backslash to escape certain characters in strings
\' - escape single quotes
\" - double quotes
\\ - backslashes
\n - start a new line
\t - tab
\r - carriage return
\b - backspace
\f - form feed
Creating a string. Backslash usage.
const str = '01234';
console.log(str.substring(1, 3)); // "12"
Similiar to slice but negative indexes are counted as 0.
str.replace("word or regex", "replacement");
const str = "Find the word or word";
str.replace("word or /word/i", "keys"); // Find the keys or word
To find and replace words in a string. Can use regex, replaces only the first occurence.
str.toUpperCase();
const str = "Wow a String!";
str.toUpperCase(); // "WOW A STRING!"
Returns a string converted to uppercase.
const fullNumber = '2034399002125581';
const last4Digits = fullNumber.slice(-4);
const maskedNumber = last4Digits.padStart(fullNumber.length, '*');
console.log(maskedNumber);
// Expected output: "************5581"
Pads the current string with another string (if needed). Padding is applied to the start of the string.
"Red Car".includes("red"); // returns false
Returns true, if contains a specified value, otherwise false.
str.length
str.slice(start, end)
str.substring(start, end)
str.substr(start, end)
str.replace("string","word")
str.replaceAll(regex, "word")
str.toUpperCase()
str.toLowerCase()
str.concat("string1", "string2")
str.trim()
str.trimStart()
str.trimEnd()
str.padStart(4, "x")
str.padEnd(4, "x")
str.charAt(index)
str.charCodeAt(index)
str.split(separator, limit)
str.indexOf("word", starting index)
str.lastIndexOf("word", starting index)
str.search(regex or string)
str.match(regex)
str.matchAll(regex)
str.includes("string", starting index)
str.startsWith("word")
str.endsWith("word")
To manipulate and represent strings.
str.concat(str1);
str.concat(str1, str2);
str.concat(str1, str2, /* …, */ strN);
To join two or more strings and return a new one.
str.split(separator, limit);
const str = 'This is a string';
console.log(str.split(" ")); // ["This", "is", "a", "string"]
Takes a string into an ordered array of substrings.
const str = 'This is a string';
str.charAt(1); // "h"
Returns the character at a specified index.
const str = 'This is a string.';
const regex = /[^\w\s]/g; // Any character that is not a word character or whitespace
str.search(regex); // Expected output: 16
str[str.search(regex)]; // Expected output: "."
Returns the index of the first match, can use a regex.
const str = 'Wow a string!';
const regex = /[A-Z]/g;
const found = str.match(regex);
console.log(found); // Expected output: Array ["W"]
Retrieves the result of matching a string against a regular expression.
const regexp = /t(e)(st(\d?))/g;
const str = 'test1test2';
const array = [...str.matchAll(regexp)];
console.log(array[0]);
// Expected output: Array ["test1", "e", "st1", "1"]
console.log(array[1]);
// Expected output: Array ["test2", "e", "st2", "2"]
Method returns an iterator of all results matching a string against a regular expression, including capturing groups.
let pet = "dog";
let sentence = `He has a ${pet}`; // He has a dog
Template literals are string literals allowing embedded expressions. You can use multi-line strings and string interpolation features. Made need backslashes. \
str.slice(start, end);
const str = '01234';
console.log(str.slice(-3)); // "234"
The slice() method extracts a section of a string and returns a new string using indexes. Negative indexes count from the end.
str.replaceAll("word or regex", "replacement");
const str = "Find the word or word";
str.replaceAll("word or /word/ig", "keys"); // Find the keys or keys
Similiar to replace but replaces all occurences. Must have a global (g) flag or an error is thrown.
str.toLowerCase();
const str = "Wow a String!";
str.toLowerCase(); // "wow a string!"
Returns a string converted to lowercase.
const str1 = 'Breaded Mushrooms';
console.log(str1.padEnd(25, '.'));
// Expected output: "Breaded Mushrooms........"
Pads the current string with another string (if needed). Padding is applied to the end of the string.
const str = 'This is a string';
str.charCodeAt(1); // 104
Returns an integer representing UTF-16 code unit.
const str = 'Saturday night plans';
str.startsWith('Sat'); // Expected output: true
str.startsWith('Sat', 3); // Expected output: false
Returns true, if a string begins with a specified value, otherwise false.