Open In App

TypeScript String includes() Method

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:

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.




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.




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
Article Tags :