js Intermediate
Search through string in JavaScript
Open this example in our interactive code editor to experiment with it.
Try the EditorFind the words and sentences
const sentence = "The quick brown fox jumps over the lazy dog and the cat";
// Search for a single character
sentence.includes("q"); // true
// Search for a word
sentence.includes("fox"); // true
// Search for a word that doesn't exist
sentence.includes("bird"); // false
// Search is case-sensitive
sentence.includes("Fox"); // false
// Search for a whole sentence
sentence.includes("jumps over the lazy dog"); // true
// Start searching from a specific index
sentence.includes("quick", 10); // false
sentence.includes("quick", 4); // true
Summary
- The
.includes()method on strings returnstrueif the search string is found anywhere within the original string, andfalseotherwise. - The search is case-sensitive, so
"Fox"and"fox"are treated as different values. - You can pass an optional second argument to specify the position in the string to begin searching from. This is useful when you want to skip a portion of the string.
.includes()works for searching single characters, words, or entire sentences, making it a versatile tool for string searching in JavaScript.
Try this in our interactive code editor
Practice hands-on with our built-in code sandbox.
Open Code Editor
Adrian Bigaj
Creator of BigDevSoon
Full-stack developer and educator passionate about helping developers build real-world skills through hands-on projects. Creator of BigDevSoon, a vibe coding platform with 21 projects, 100 coding challenges, 40+ practice problems, and Merlin AI.
Related Pills
js Intermediate
JavaScript multiple condition checks
Checking multiple values in JavaScript often leads to redundant, boilerplate code - let's use .includes method to fix that.
js Intermediate
Convert JavaScript data structures
Converting data structures is a day-to-day job as a Developer.
js Intermediate
ECMAScript new features
Null coalescing operator, optional chaining, and Array.flat method are all amazing.