JavaScript Code

JavaScript String includes()


String includes()

str.includes(search, start)
  • 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. check if string value in name contains search string 'pl'

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

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