TypeScript String includes() Method
Last Updated :
28 Feb, 2024
TypeScript includes() a predefined string method that comes from JavaScript. It checks if a given string contains any specified substring or not based on this it returns a boolean value.
Syntax
string.includes(searchValue, fromIndex?: number): boolean;
Parameters:
- searchValue: Here you have to pass the substring that you want to find out within the string.
- fromIndex (Optional): The starting point, for the search. By default, it begins at 0 indicating that the search commences, from the start of the string.
Return Value:
If the given searchValue is found within the string it returns true otherwise it returns false.
Example 1: In this example, we are checking for substring presence.
Javascript
let str: string = "Welcome to GFG" ;
let hasString: boolean = str
.includes( "GFG" );
console.log(hasString);
|
Output:
true
Example 2: In this example we are demonstrating case sensitivity.
Javascript
let str: string = "TypeScript" ;
let check1: boolean = str
.includes( "Typescript" );
let check2: boolean = str
.includes( "TypeScript" );
console.log(check1);
console.log(check2);
|
Output:
false
true
Share your thoughts in the comments
Please Login to comment...