const str = "Hello World"; // Original string for all examples
// Create
const str = "Hello World"; // "Hello World"
const str2 = String(123); // "123"
// Case (RETURNS NEW STRING)
str.toLowerCase(); // "hello world"
str.toUpperCase(); // "HELLO WORLD"
// Search/Find (RETURNS INDEX OR BOOLEAN)
str.indexOf("World"); // 6
str.lastIndexOf("l"); // 9
str.search("World"); // 6 (regex supported)
str.includes("Hello"); // true
str.startsWith("Hello"); // true
str.endsWith("World"); // true
// Extract/Slice (RETURNS NEW STRING)
str.slice(0, 5); // "Hello"
str.slice(-5); // "World"
str.substring(0, 5); // "Hello"
str.charAt(1); // "e"
str.charCodeAt(1); // 101 (UTF-16 code)
str.at(-1); // "d" (last character)
// Replace (RETURNS NEW STRING)
str.replace("World", "JS"); // "Hello JS"
str.replaceAll("l", "x"); // "Hexxo Worxd"
// Split/Join (RETURNS ARRAY OR STRING)
str.split(" "); // ["Hello", "World"]
str.split(""); // ["H", "e", "l", "l", "o", " ", "W", "o", "r", "l", "d"]
str.split(" ", 1); // ["Hello"] (limit)
// Trim/Pad (RETURNS NEW STRING)
" Hello ".trim(); // "Hello"
" Hello ".trimStart(); // "Hello "
" Hello ".trimEnd(); // " Hello"
"5".padStart(3, "0"); // "005"
"5".padEnd(3, "0"); // "500"
// Repeat/Concat (RETURNS NEW STRING)
"Hi".repeat(3); // "HiHiHi"
str.concat(" !", " How are you?"); // "Hello World ! How are you?"
str + " !"; // "Hello World !"
// Compare (RETURNS NUMBER)
"a".localeCompare("b"); // -1 (a < b)
"b".localeCompare("a"); // 1 (b > a)
"a".localeCompare("a"); // 0 (equal)
// Convert (RETURNS VARIOUS TYPES)
"123".toString(); // "123"
"123".valueOf(); // "123"
Number("123"); // 123
parseInt("123px"); // 123
parseFloat("123.45px"); // 123.45
// Properties
str.length; // 11
str[0]; // "H" (bracket notation)