Javascript String startsWith()

The startsWith() method returns true if a string begins with specified character(s). If not, it returns false.

Example


startsWith() Syntax

The syntax of the startsWith() method is:

str.startsWith(searchString, position)

Here, str is a string.


startsWith() Parameters

The startsWith() method takes in :

  • searchString - The characters to be searched for at the beginning of str.
  • position (optional) - The position in str to start searching for searchString. Default value is 0.

startsWith() Return Value

  • Returns true if the given characters are found at the beginning of the string.
  • Returns false if given characters are not found at the beginning of the string.

Note: The startsWith() method is case sensitive.


Example: Using startsWith() method

Output

true
true
false
true

Recommended Reading: JavaScript String endsWith()