

We have written the output of the includes() method to the web browser console log, for demonstration purposes, to show what the includes() method returns. We have then invoked the includes() method of the totn_string variable to determine if a substring is found within totn_string. In this example, we have declared a variable called totn_string that is assigned the string value of 'TechOnTheNet'. It's worth noting though that it will execute that code for each item that is in the list that returns true so make sure you compensate for that in the code you want executed.Įdit- you could also convert this to a function set it up to pass arguments that are the multiple things you check to see if the string includes and just have it return true or false and you can do whatever with that information outside of the function.Let's take a look at an example of how to use the includes() method in JavaScript.įor example: var totn_string = 'TechOnTheNet' You can also add what to do if it's false in the if statement as well. execute a function, change a variable etc. It will return true if any of those items are in that string and then you can have it do whatever. Const text = ' does this include item3? '

includes to search for multiple things in a string at once.ĭepending on what you are trying to do with it just run a for loop that runs through an array of items you want to check a string for using. I didn't see it on the results and it's one of the easiest ways to use. Var res4 = text.filter(data => data.includes(conditions) || data.includes(conditions)) Ĭonsole.log(res4) // ĭef an old thread but still getting newer replies. You may use again the same filters for a different check

Var res3 = text.filter(data => data.includes(conditions) & data.includes(conditions)) filter(data => data.includes(conditions) || data.includes(conditions) || data.includes(conditions)) Ĭonsole.log(res1) // filter(data => data.includes(conditions) || data.includes(conditions) || data.includes(conditions)) filter() works with an array and returns a filtered array (which I find easier to use too). Like if you want to use the same filters for all of your checks.Īctually. Not the best answer and not the cleanest, but I think it's more permissive.
