JavaScript string contains substring or characters | include function

Using the includes() function you can check whether a string contains a substring in JavaScript. This method returns true if the string contains the characters, and false if not.

Usually, programmers would expect a String.contains() method, but there doesn’t exist in JS.

Syntax

Examples of the JavaScript string contains a substring

Let’s see multiple examples.

if the string contains

Output:

JavaScript string contains substring or characters

Check javascript string contains a character

Let’s find “hello” in your_string using a indexOf() method in js.

The string contains case insensitive in JS

Add .toLowerCase() after referrer. This method turns the string in a lower case string. Then, use includes() function.

Check below code for it:-

Do comment if you know other ways and have any doubts.

Note: The All JS Examples codes are tested on the Safari browser (Version 12.0.2) and Chrome.
OS: macOS 10.14 Mojave
Code: HTML 5 Version


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.