Javascript String includes()

Introduction

Javascript String includes() searches a string for a given substring and return a boolean indicating whether it is included.

includes() checks the entire string.

str.includes(searchString[, position])
  • searchString - a string to be searched for within str.
  • position - Optional, the position to start searching for searchString. (Defaults to 0.)

The includes() method is case sensitive.

console.log('B'.includes('b'));
let message = "foobarbaz"; 
console.log(message.includes("bar"));    // true 
console.log(message.includes("qux"));    // false 

The includes() method accept an optional second argument that indicates the position to start searching.

This method will start searching from that position and go toward the end of the string, ignoring everything before the start position.

let message = "foobarbaz"; 
console.log(message.includes("bar"));       // true 
console.log(message.includes("bar", 4));    // false 
const str = 'this is a test.CSS HTML Java' 

console.log(str.includes('this is'));
console.log(str.includes('css'));
console.log(str.includes('CSS'));
console.log(str.includes('test', 1));
console.log(str.includes('test', 0));
console.log(str.includes('TEST'));
console.log(str.includes(''));



PreviousNext

Related