50% off all plans with SPRING50
js Intermediate

Search through string in JavaScript

Search through string in JavaScript
Photo by Shane Aldendorff on Unsplash
String .includes() Demo
Interactive editor — edit code and see live results Open in Editor

Find 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 returns true if the search string is found anywhere within the original string, and false otherwise.
  • 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
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.