Skip to content
Related Articles

Related Articles

JavaScript | String includes()
  • Last Updated : 27 Jan, 2018

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:




// 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>


Output:

true

Program 2:




<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>   


Output:

false

Program 3:




<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>   


Output:

true

full-stack-img

My Personal Notes arrow_drop_up
Recommended Articles
Page :