JavaScript startsWith() Method | Tutorial
JavaScript Reference
Overview - JavaScript and HTML DOM Reference
JavaScript Objects
- JavaScript Array Object
- JavaScript Boolean Object
- JavaScript Date Object
- JavaScript Math Object
- JavaScript Number Object
- JavaScript String Object
- JavaScript RegExp Object
- JavaScript Global Properties/Functions
- JavaScript Operators
- JavaScript Error
Browser Objects
DOM Objects
- HTML DOM Document Object
- HTML DOM Element Object
- HTML DOM Attribute Object
- HTML DOM Event Object
- HTML DOM Console Object
- CSSStyleDeclaration Object
- DOM HTMLCollection
JavaScript startsWith() Method
JavaScript String Object Reference
Example
Check if a string starts with "Hello":
var str = "Hello world, welcome to the Tutorial.";
var n = str.startsWith("Hello");
n output:
true
Definition and Usage
The startsWith() method is used to check whether a string begins with a 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 specify the first browser version that fully supports the method.
| Method | Chrome | Edge | Firefox | Safari | Opera |
|---|---|---|---|---|---|
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 position in the string to start 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
Check if a string starts with "world" from index position 6:
var str = "Hello world, welcome to the Tutorial.";
var n = str.startsWith("world", 6);
n output:
true
YouTip