JavaScript String Methods: The Ultimate Cheat Sheet
Strings are everywhere. Master these methods and you'll write cleaner code.
Search & Check
const str = ' Hello, World! Welcome to JavaScript. ' ;
// Includes (ES6+ — use this!) str . includes ( ' World ' ); // true str . includes ( ' world ' ); // false (case-sensitive) str . includes ( ' Python ' ); // false
// Starts with / ends with str . startsWith ( ' Hello ' ); // true str . startsWith ( ' World ' , 7 ); // true (from index 7) str . endsWith ( ' Script. ' ); // true str . endsWith ( ' . ' ); // true
// Index of first/last occurrence str . indexOf ( ' o ' ); // 4 (first 'o') str . indexOf ( ' o ' , 5 ); // 8 (first 'o' after index 5) str . lastIndexOf ( ' o ' ); // 22 (last 'o') str . indexOf ( ' xyz ' ); // -1 (not found)
// Match with regex str . match ( /o/g ); // ['o', 'o', 'o'] — all matches str .…