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:
startsWith(searchString)Parameters
| Parameter | Optional/Required | Description |
|---|---|---|
searchString | required | The 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.
startsWith(searchString, position)Parameters
| Parameter | Optional/Required | Description |
|---|---|---|
searchString | required | The characters to be searched for at the start of this string. |
position | optional | The 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,
- We define a string variable
strwith the value'Hello, world!'. - We use the
startsWith()method with the search string'Hello'. - The result, a boolean indicating whether the string starts with
'Hello', is stored in the variableresult. - We log
resultto the console usingconsole.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,
- We define a string variable
strwith the value'Hello, world!'. - We use the
startsWith()method with the search string'world'and the position7. - The result, a boolean indicating whether the string starts with
'world'at position7, is stored in the variableresult. - We log
resultto the console usingconsole.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,
- We define a string variable
strwith the value'JavaScript'. - We use the
startsWith()method with the search string'Java'. - The result, a boolean indicating whether the string starts with
'Java', is stored in the variableresult. - We log
resultto the console usingconsole.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.