JavaScript - How To Check Whether a String Contains a Substring? Last Updated : 02 Dec, 2024 Comments Improve Suggest changes Like Article Like Report Here are the different methods to check whether a string contains a substring in JavaScript.1. Using includes() MethodThe includes() method is the most simple and modern way to check if a string contains a specific substring. It returns true if the substring is found within the string, and false otherwise. JavaScript let s = "JavaScript is awesome!"; let res = s.includes("awesome"); console.log(res); Outputtrue includes() checks if the substring ("awesome") exists within the string ("JavaScript is awesome!").It returns a boolean value (true or false).This method is case-sensitive, so "awesome" is different from "Awesome".2. Using indexOf() MethodThe indexOf() method searches for a substring within a string and returns the index of the first occurrence. If the substring is not found, it returns -1. JavaScript let s = "JavaScript is awesome!"; let res = s.indexOf("awesome") !== -1; console.log(res); Outputtrue indexOf() returns the index of the first occurrence of the substring.If the substring is found, the result will be an index (>= 0), otherwise -1.You can compare the result to -1 to check if the substring is present.3. Using Regular Expressions (RegExp)You can also use regular expressions to check if a substring exists within a string. This is especially useful when you want to perform case-insensitive checks or match more complex patterns. JavaScript let s = "JavaScript is awesome!"; let res = /awesome/i.test(s); console.log(res); Outputtrue /awesome/i.test(str) creates a regular expression to check if the word "awesome" exists in the string, ignoring case (i flag).The test() method returns a boolean (true or false).Regular expressions are more powerful but can be overkill for simple substring checks.4. Using search() MethodThe search() method allows you to search for a substring or regular expression pattern within a string. It returns the index of the first match or -1 if no match is found. JavaScript let s = "JavaScript is awesome!"; let res = s.search("awesome") !== -1; console.log(res); Outputtrue search() works similarly to indexOf() but can accept regular expressions.If the substring is found, it returns the index, otherwise -1.Like indexOf(), you compare the result to -1 to check for the substring.5. Using split() MethodThe split() method splits a string into an array of substrings based on a separator. You can use it to check if the string can be split into an array that includes the substring. JavaScript let s = "JavaScript is awesome!"; let res = s.split("awesome").length > 1; console.log(res); Outputtrue split("awesome") divides the string at each occurrence of "awesome".If the substring exists, the resulting array will have more than one element, and the length will be greater than 1.This method can be useful for checking multiple substrings at once.Which Approach to Choose in Which Case?MethodWhen to UseWhy Choose Itincludes()When you need a simple and modern way to check for a substring.Clean, easy-to-read syntax and good for most use cases.indexOf()When you need the index of the first occurrence of a substring.Useful for finding the position of the substring or checking existence.RegExpWhen you need more complex patterns or case-insensitive searches.Great for pattern matching and flexible, powerful checks.search()When working with regular expressions or need the index.Similar to indexOf() but allows for regular expression matching.split()When you want to check the existence of multiple substrings or split at a specific pattern.Useful for checking multiple substrings or patterns in one go. Comment More infoAdvertise with us Next Article JavaScript - How To Check Whether a String Contains a Substring? S souravsharma098 Follow Improve Article Tags : JavaScript Web Technologies javascript-string Similar Reads JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav 11 min read Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De 5 min read React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications 15+ min read JavaScript Interview Questions and Answers JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as 15+ min read React Tutorial React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon 8 min read Domain Name System (DNS) DNS is a hierarchical and distributed naming system that translates domain names into IP addresses. When you type a domain name like www.geeksforgeeks.org into your browser, DNS ensures that the request reaches the correct server by resolving the domain to its corresponding IP address.Without DNS, w 8 min read NodeJS Interview Questions and Answers NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net 15+ min read HTML Interview Questions and Answers HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML 14 min read What is an API (Application Programming Interface) In the tech world, APIs (Application Programming Interfaces) are crucial. If you're interested in becoming a web developer or want to understand how websites work, you'll need to familiarize yourself with APIs. Let's break down the concept of an API in simple terms.What is an API?An API is a set of 10 min read Introduction of Firewall in Computer Network A firewall is a network security device either hardware or software-based which monitors all incoming and outgoing traffic and based on a defined set of security rules it accepts, rejects, or drops that specific traffic. It acts like a security guard that helps keep your digital world safe from unwa 10 min read Like