Check if a String Contains a Substring in JavaScript

Written: August 22, 2026

Modern JavaScript makes substring checks easy with String.prototype.includes. Older code often used indexOf !== -1. Regex is for patterns, not for every simple contains check.

Examples

const text = "Appium parallel sessions";

console.log(text.includes("parallel")); // true
console.log(text.indexOf("Appium") !== -1); // true
console.log(/session/i.test(text)); // true (case-insensitive)

Gotchas

includes is case-sensitive. Normalize with toLowerCase() on both sides when you want a case-insensitive contains.

For user search boxes, trim input and decide whether empty query should match everything or nothing.

Keep learning

If this walkthrough on javascript string contains substring helped, open the code again and change one input or assumption. Small experiments beat rereading the same example.

Want more step-by-step tutorials like this? Browse blog.xqa.io — and tell us which topic you want next.

Advertisement

Previous Article

Simple Python Calculator Code

Next Article

C Program to Print Day of Week (1 to 7)

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Subscribe to our Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨