

How to check if a string contains a specific substring using the indexOf() method.Syntax breakdown of the indexOf() method in JavaScript.How to check if a string contains a specific substring using the includes() method.Syntax breakdown of the includes() method in JavaScript.

Here is what we will cover in more detail: How to use the built-in indexOf() JavaScript method.How to use the built-in includes() JavaScript method.In this article, you will learn two different ways you can check if a string contains a substring using JavaScript methods. Thankfully, there are a few quick ways to achieve this with JavaScript. Specifically, you might need to check whether a word contains a specific character or a specific set of characters. A substring is a string inside another string. it would return false if you check the inclusion of string intelligent in title string.When you're working with a JavaScript program, you might need to check whether a string contains a substring. The includes() method is case sensitive i.e. Now, if you console.log the textIsPresent variable, you should get the output as true let title = '12 Ways to Tell Someone Is Really Intelligent'Ĭonsole.log(textIsPresent) // Output: true Let textIsPresent = title.includes('Intelligent') To check whether variable title contains string Intelligent, we can use () as follows let title = '12 Ways to Tell Someone Is Really Intelligent' Let’s say you following string let title = '12 Ways to Tell Someone Is Really Intelligent' Old way: Before ES6, () method was used to check whether a string contains a substring, which returns -1 when a substring cannot be found. It returns a boolean value of true if the string is found, otherwise false. () method checks if one string may be found within another string. In this tutorial, I am going to show you a very simple, fast, and modern(ES6) way to check whether a string contains a substring in JavaScript.
