← flaviocopes.com

How to check if a string starts with another in JavaScript

Published Sep 29 2018

ES6, introduced in 2015, added the startsWith() method to the String object prototype.

This is the way to perform this check in modern JavaScript

This means you can call startsWith() on any string, provide a substring, and check if the result returns true or false:

'testing'.startsWith('test') //true
'going on testing'.startsWith('test') //false

This method accepts a second parameter, which lets you specify at which character you want to start checking:

'testing'.startsWith('test', 2) //false
'going on testing'.startsWith('test', 9) //true

Wanna go from noobie to expert?

I wrote an entire book on this topic 👇

I also got a super cool course 👇

© 2023 Flavio Copes Flavio Copes using Notion to Site Notion to Site. Follow on Twitter

Solopreneur? Wannabe? Adventure awaits