JavaScript Code

JavaScript String includes()


String includes()

In JavaScript, String.includes() method is used to check if a search string is present in the string or not.

Syntax

str.includes(search, start)

where

  • str is a string.
  • search is a string.
  • start is an integer, index position.

includes() method returns true if the string str contains the specified search string, else it returns false. If start is provided, the string str from the start position is considered for checking.

Examples

1. In the following program, we check if string value in name contains search string 'pl'.

let name = 'apple';
let output = name.includes('pl');
console.log(output);

2. In the following program, we check if string value in name contains search string 'nana'.

let name = 'apple';
let output = name.includes('nana');
console.log(output);

3. Check if string value in name contains search string 'ap' from a start position of 2.

let name = 'apple';
let output = name.includes('ap', 2);
console.log(output);


Copyright @2022