JavaScript String startsWith() Tutorial

In this section, we will learn what the String startsWith() method is and how to use it in JavaScript.

JavaScript String startsWith() Method

The String startsWith() method is used to check a string content and see if it starts with a specific value or not.

JavaScript String startsWith() Syntax

string.startsWith(searchvalue, start)

String startsWith() Method Parameters:

The startsWith() method takes two arguments:

  • The first argument is the string value that we want to search for in the target text content and see if that text starts with this string value.
  • The second argument is the position from which we want this search to start from. This second argument is optional and, if ignored, then the search begins from the index 0 for the text content (which is the first character).

String startsWith() Method Return Value:

The return value of this method is either true or false.

If the value is true, that means the target text content starts with the value we set as the first argument of this method.

If the value is false, that means the target text content starts with another string value (other than the one we’ve set as the argument of the method).

Example: using startsWith() method in JavaScript

const id = "If you work hard, then dreams will come true";

const res = id.startsWith("If you work hard");

console.log(res);

Output:

true
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies