Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript startsWith() Method

JavaScript startsWith() Method

String Object Reference JavaScript String object

Example

Check whether a string starts with "Hello":

var

str

=

"

Hello world, welcome to the Runoob.

"

;

var

n

=

str

.

startsWith

(

"

Hello

"

)

;

n Output result:

true

Try it »


Definition and Usage

The startsWith() method is used to check whether a string begins with a specified substring.

If it begins with the specified substring, it returns true; 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 value

Parameters Description
searchvalue Required, the string to search for.
start Optional, the starting position of the search, defaulting to 0.

Return Value

Type Description
Boolean

If the string begins with the specified substring, it returns true; 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 Runoob.

"

;

var

n

=

str

.

startsWith

(

"

world

"

,

6

)

;

n Output result:

true

Try it »


String Object Reference JavaScript String object