Checking Substring in JavaScript: 3 Ways for Beginners

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

"Does this string contain that word?" — a question that haunts every beginner

\n

You're writing a script, receiving a response from a server, or simply filtering a list of cities. And suddenly you're stuck: how to check if a string contains a specific substring in JavaScript? This question is a real hit on Stack Overflow, asked by thousands of developers. And for good reason: strings are the foundation of any application, and the ability to quickly search for substrings in them is a basic skill.

\n

In this tutorial, we'll cover three working methods: from the most modern (and everyone's favorite) to the classic one with extra features. You'll learn when to use which method and how to write code that you won't be ashamed to show at an interview.

\n\n

Method #1: includes() — simplicity and elegance

\n

If you're writing code in modern JavaScript (ES6 and above), then includes() is your best friend. This method returns true or false and reads like a plain English question: "Does the string include the substring?".

\n\n

Syntax

\n
string.includes(searchString, position)
\n
    \n
  • searchString — what we are looking for (required).
  • \n
  • position — optional parameter, the index from which to start the search (default is 0).
  • \n
\n\n

Example in action

\n
const message = "Hello, world! JavaScript is cool.";\n\nconsole.log(message.includes("JavaScript")); // true\nconsole.log(message.includes("Python"));     // false\nconsole.log(message.includes("world", 10));    // false (search starting from the 10th character)\n
\n

Note: the method is case-sensitive. "javascript" and "JavaScript" are different strings. If you need to ignore case, simply convert both strings to lowercase:

\n
const text = "Learning JavaScript";\nconst keyword = "javascript";\n\nconsole.log(text.toLowerCase().includes(keyword.toLowerCase())); // true\n
\n\n

When to use

\n
    \n
  • Form validation (checking email for "@").
  • \n
  • Searching through a list (filtering products).
  • \n
  • Any simple containment check.
  • \n
\n\n

Method #2: indexOf() — a classic that never dies

\n

Before includes() appeared, everyone used indexOf(). It doesn't just say "yes/no", but returns the index of the first occurrence of the substring. If the substring is not found, it returns -1.

\n\n

Syntax

\n
string.indexOf(searchValue, fromIndex)
\n\n

Example

\n
const sentence = "I love JavaScript and Python.";\n\nconsole.log(sentence.indexOf("love")); // 2\nconsole.log(sentence.indexOf("Ruby"));  // -1\n\n// Checking for containment\nif (sentence.indexOf("JavaScript") !== -1) {\n    console.log("Yes, the string contains JavaScript!");\n}\n
\n\n

Subtle point: why not !== -1?

\n

Many beginners write if (str.indexOf("word")) — and this is a mistake. If the substring is at the very beginning (index 0), the condition if (0) evaluates to false. Always compare with -1.

\n\n

When to use indexOf() instead of includes()

\n
    \n
  • When you need the index of the start of the substring (e.g., to cut out part of a string).
  • \n
  • Working with legacy code (IE and ancient browser versions).
  • \n
  • When you need to find all occurrences (in a loop changing fromIndex).
  • \n
\n\n

Method #3: search() and RegExp — when you need flexibility

\n

But what if you need to find not just a word, but a pattern? For example, "all words starting with 'JS'" or "a phone number in the format +7-xxx-xxx-xx-xx". This is where regular expressions and the search() method come to the rescue.

\n\n

Syntax

\n
string.search(regexp)
\n

Blogs

Book Recommendations