"Does this string contain that word?" — a question that haunts every beginner
\nYou'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.
\nIn 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\nMethod #1: includes() — simplicity and elegance
\nIf 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?".
Syntax
\nstring.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
Example in action
\nconst 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\nNote: the method is case-sensitive. "javascript" and "JavaScript" are different strings. If you need to ignore case, simply convert both strings to lowercase:
const text = "Learning JavaScript";\nconst keyword = "javascript";\n\nconsole.log(text.toLowerCase().includes(keyword.toLowerCase())); // true\n\n\nWhen to use
\n- \n
- Form validation (checking email for "@"). \n
- Searching through a list (filtering products). \n
- Any simple containment check. \n
Method #2: indexOf() — a classic that never dies
\nBefore 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.
Syntax
\nstring.indexOf(searchValue, fromIndex)\n\nExample
\nconst 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\nSubtle point: why not !== -1?
\nMany 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.
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
Method #3: search() and RegExp — when you need flexibility
\nBut 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.
Syntax
\nstring.search(regexp)\n
Blogs
Book Recommendations
Read also
C# vs Python for Backend: What to Choose in Practice in 2024
Concurrency and Multithreading in PHP: Advanced Level
Open source contribution: how to start contributing to Java projects
Python Decorators: What They Are, How They Work, and Why Use Them
Deploying Go on Kubernetes: A Basic Practical Guide