How to check if a string starts with a specific string or a character in JavaScript?

Published May 1, 2021

To check if a string starts with another string, you can use the startsWith() string method in JavaScript.

For example, let's say you have a string called Hello World! and wants to check if the string starts with the word Hello, you can pass this string as an argument to the startsWith() method like this,

// a string
const str = "Hello World!";

// string to check
const strToCheck = "Hello";

// use startsWith() string method to check
// if string starts with another string
str.startsWith(strToCheck); // true
  • The method returns a boolean value true if it starts with the supplied string and false if not.

See the code live in JSBin

Feel free to share if you found this useful 😃.

Share on: Facebook Twitter