JavaScript Tutorials

JavaScript String startsWith()
Syntax & Examples

String.startsWith() method

The startsWith() method of the String class in JavaScript determines whether a string begins with the characters of a specified string, returning true or false as appropriate.


Syntax of String.startsWith()

There are 2 variations for the syntax of String.startsWith() method. They are:

1.
startsWith(searchString)

Parameters

ParameterOptional/RequiredDescription
searchStringrequiredThe characters to be searched for at the start of this string.

This method checks if the string starts with the specified searchString.

Returns value of type Boolean.

2.
startsWith(searchString, position)

Parameters

ParameterOptional/RequiredDescription
searchStringrequiredThe characters to be searched for at the start of this string.
positionoptionalThe position in this string at which to begin searching for searchString. Defaults to 0.

This method checks if the string starts with the specified searchString at the given position.

Returns value of type Boolean.



✐ Examples

1 Using startsWith() method with a search string

In JavaScript, we can use the startsWith() method to check if a string starts with a specified substring.

For example,

  1. We define a string variable str with the value 'Hello, world!'.
  2. We use the startsWith() method with the search string 'Hello'.
  3. The result, a boolean indicating whether the string starts with 'Hello', is stored in the variable result.
  4. We log result to the console using console.log() method.

JavaScript Program

const str = 'Hello, world!';
const result = str.startsWith('Hello');
console.log(result);

Output

true

2 Using startsWith() method with a search string and a position

In this example, we use the startsWith() method to check if a string starts with a specified substring at a given position.

For example,

  1. We define a string variable str with the value 'Hello, world!'.
  2. We use the startsWith() method with the search string 'world' and the position 7.
  3. The result, a boolean indicating whether the string starts with 'world' at position 7, is stored in the variable result.
  4. We log result to the console using console.log() method.

JavaScript Program

const str = 'Hello, world!';
const result = str.startsWith('world', 7);
console.log(result);

Output

true

3 Using startsWith() method with a search string and a default position

In this example, we use the startsWith() method to check if a string starts with a specified substring, without specifying a position (defaults to 0).

For example,

  1. We define a string variable str with the value 'JavaScript'.
  2. We use the startsWith() method with the search string 'Java'.
  3. The result, a boolean indicating whether the string starts with 'Java', is stored in the variable result.
  4. We log result to the console using console.log() method.

JavaScript Program

const str = 'JavaScript';
const result = str.startsWith('Java');
console.log(result);

Output

true

Summary

In this JavaScript tutorial, we learned about startsWith() method of String: the syntax and few working examples with output and detailed explanation for each example.