JavaScript startsWith()Method
Examples
Check if a string starts with "Hello":
var str = "Hello world, welcome to the Example.";
var n = str.startsWith("Hello");
nOutput:
true
Try it »
Definition and Usage
The startsWith() method is used to detect whether a string starts with the specified substring.
It returns true if the string starts with the specified substring, otherwise false.
The startsWith() method is case-sensitive.
Browser Support
The numbers in the table indicate the first browser version that supports this property.
| Method | |||||
|---|---|---|---|---|---|
| startsWith() | 41 | 12.0 | 17 | 9 | 28 |
Syntax
string.startsWith(searchvalue, start)
Parameter Values
| Parameter | Description |
|---|---|
| searchvalue | Required. The string to search for. |
| start | Optional. The starting position of the search, default is 0. |
Return Value
| Type | Description |
|---|---|
| Boolean | Returns true if the string starts with the specified substring, otherwise false. |
Technical Details
| JavaScript Version: | ECMAScript 6 |
|---|
More Examples
Example
Check whether it starts with "world" from index position 6:
var str = "Hello world, welcome to the Example.";
var n = str.startsWith("world", 6);
nOutput:
true
Try it »
Other Extensions
JavaScript String Object