JavaScript | String includes()

str.includes() function is used to check if the argument string is present in the given string or not. This function is case sensitive. The syntax of this function is as follows:

str.includes(searchString, position)

Arguments The first argument to this function searchString is the string to be searched in the given string. The second argument to this function position determines the starting index in the base string from where the searchString is to be searched.

Examples for the above function are provided below:



Example 1:

print('Departed Train'.includes('ed Trai')); 

Output:

true

In this example the function includes() searches for ed Trai. Since it is found in the given string therefore this function returns true.

Example 2:

print('Departed Train'.includes('train')); 

Output:

false

In this example the function includes() searches for train. Since it is not found in the given string therefore this function returns false.

Example 3:

print('Departed Train'.includes('Train')); 

Output:

true

In this example the function includes() searches for Train. Since it is found in the given string therefore this function returns true.

Codes for the above function are provided below:


Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// JavaScript to illustrate includes() function
<script>
function func() {
  
    // Original string
    var str = 'Departed Train';
  
    // Finding the search string in the given string 
    var value = str.includes('ed Trai');
    document.write(value);
}
func();
</script>

chevron_right


Output:

true

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
// JavaScript to illustrate includes() function
function func() {
  
    // Original string
    var str = 'Departed Train';
  
    // Finding the search string in the given string 
    var value = str.includes('train');
    document.write(value); 
}
func();
</script>   

chevron_right


Output:

false

Program 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
// JavaScript to illustrate includes() function
function func() {
  
    // Original string
    var str = 'Departed Train';
  
    // Finding the search string in the given string 
    var value = str.includes('Train');
    document.write(value);
}
func();
</script>   

chevron_right


Output:

true


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.