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

It is very common to check whether a string starts with another substring. Learn how to perform this check in JavaScript

ES6, launched in 2015, addedstartsWith()The method of the String object prototype.

this isThisWays to perform this check in modern JavaScript

This means you can callstartsWith()On any string, provide a substring and check if the result is returnedtrueorfalse:

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

This method accepts a second parameter, which allows you to specify the character to start checking:

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

Download mine for freeJavaScript beginner's manual


More js tutorials: