Latest web development tutorials
 

JavaScript String startsWith() Method

< JavaScript String Reference

Example

Check if a string starts with "Hello":

var str = "Hello world, welcome to the universe.";
var n = str.startsWith("Hello");

The result of n will be:

true
Try it Yourself »

More "Try it Yourself" examples below.


Definition and Usage

The startsWith() method determines whether a string begins with the characters of a specified string.

This method returns true if the string begins with the characters, and false if not.

Note: The startsWith() method is case sensitive.


Browser Support

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. Default 0. At which position to start the search

Technical Details

Return Value: A Boolean. Returns true if the string starts with the value, otherwise it returns false
JavaScript Version: ECMAScript 6

Examples

More Examples

Check if a string starts with "world", starting the search at position 6:

var str = "Hello world, welcome to the universe.";
var n = str.startsWith("world", 6);

The result of n will be:

true
Try it Yourself »

< JavaScript String Reference