How to check if a string starts with another string in JavaScript

by Sai gowtham1min read

In this tutorial, we are going to learn two different ways to check if a string starts with a specified string.

First way

startsWith() method in JavaScript takes the search string as its first argument and check whether a given string starts with a search string and returns true or false.

syntax: String.prototype.startsWith(searchstring,position)

const str = 'this is my name'


// true

By default, the string searching starts from position 0. We can also change its position by passing the second argument.

const str = 'this is my name'

console.log(str.startsWith('my',8)) // true

console.log(str.startsWith('my')) // false

Second way

The includes() method returns true if a search string found in the given string, otherwise it returns false.

const str = 'hey buddy this is awesome place';

console.log(str.includes('is')) // true

console.log(str.includes('place')) // true

console.log(str.includes('notfound')) //  false

Note: The above two methods are case-sensitive.