Javascript - String startsWith() Method

The startsWith() method checks if a string begins with the specified string.

Description

The startsWith() method checks if a string begins with the specified string.

It returns true if the string begins with the characters, and false otherwise.

The startsWith() method is case sensitive.

Syntax

string.startsWith(searchvalue,start)

Parameter Values

Parameter Require Description
searchvalue Required. The string to search for
start Optional. Default is 0. At which position to start the search

Return

A Boolean type value. Returns true if the string starts with the value, otherwise it returns false

Example

Check if a string starts with "Hello":

Demo

//check where if the string starts with the specified value.
var str = "Hello world, this is a test.";
var n = str.startsWith("Hello");
console.log(n);/*from w w  w  .ja v  a 2 s  . c  o  m*/

//Check if a string starts with "world", starting the search at position 6:
var str = "world this is a test world .";
var n = str.startsWith("world", 6);
console.log(n);

Result