DEV Community

loading...
Cover image for How to check if a string starts with a specific string or a character in JavaScript?

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

MELVIN GEORGE
Hi, I'm Melvin George, A web developer, and a blogger.
Originally published at melvingeorge.me ・1 min read

Originally posted here!

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
Enter fullscreen mode Exit fullscreen mode
  • 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 😃.


Discussion (0)