Menu

1 / 2

JavaScript String Methods: The Ultimate Cheat Sheet

DEV Community·Alex Chen·5 months ago
#idK8J6II
#javascript#extract#beginners#tutorial#hello
Reading 0:00
15s threshold

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

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.