Open In App

String includes() Method in JavaScript

Improve
Improve
Like Article
Like
Save
Share
Report

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

Example: 

JavaScript




// 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');
    console.log(value);
}
func();
</script>


Output:

true

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




<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');
    console.log(value);
}
func();
</script>


Output:

false

Program 2: 

JavaScript




<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');
    console.log(value);
}
func();
</script>


Output:

true


Last Updated : 19 Oct, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads