SyntaxStudy
Sign Up
Home JavaScript Reference includes() / startsWith() / endsWith()

includes() / startsWith() / endsWith()

method ES6

Check whether a string contains, begins with, or ends with a given substring.

Syntax

string.includes(search) string.startsWith(str) string.endsWith(str)

Example

javascript
const url = 'https://www.example.com/page';

console.log(url.includes('example'));      // true
console.log(url.startsWith('https'));      // true
console.log(url.endsWith('/page'));        // true
console.log(url.startsWith('http', 0));   // true (offset)

// Search check
const email = 'user@gmail.com';
if (email.endsWith('@gmail.com')) {
    console.log('Gmail user');
}