JavaScript String.startsWith() Method
-
Syntax of JavaScript
string.startsWith()
: -
Example Codes: Use the
searchValue
Parameter Value for thestring.startsWith()
Method -
Example Codes: Use the
position
Parameter Value for thestring.startsWith()
Method -
Example Codes: Use a Punctuation in the
searchValue
Parameter for thestring.startsWith()
Method
The startsWith()
method takes the string and the position to find if the string starts with the searchValue
. When the start position parameter is empty, the startsWith()
method checks the index position 0.
Syntax of JavaScript string.startsWith()
:
referenceString.startsWith(searchValue);
referenceString.startsWith(searchValue,position);
Parameters
searchValue |
This is the value (string) to be searched starting from the specified index position. |
position |
This is the specified index position of the string to check the given value (string). The default value is 0. |
Return
It returns true
when a string’s specific index position
starts with the searchValue
. Else, it returns false
.
Example Codes: Use the searchValue
Parameter Value for the string.startsWith()
Method
The JavaScript startsWith()
method uses the searchValue
parameter to determine whether the string starts with a specified character or a regular expression.
In the example below, we didn’t pass the position
parameter. The string.startsWith()
takes the string and returns a boolean value by checking if the string starts with the searchValue
.
let refString = "Hello World!";
let regExp = 'Hello';
let searchString = refString.startsWith(regExp);
console.log(searchString);
Output:
true
Example Codes: Use the position
Parameter Value for the string.startsWith()
Method
When a position is specified, string.startsWith()
returns a boolean value based on the match between searchValue
and the characters of the string. In the example below, we passed the searchValue
with the position
to check if the value starts the specified index position.
let string = "Typing the code.";
let strWord = 'code';
let searchString = string.startsWith(strWord, 11);
console.log(searchString);
Output:
true
Example Codes: Use a Punctuation in the searchValue
Parameter for the string.startsWith()
Method
The string.startsWith()
method is case-sensitive. The startsWith()
method returns true by checking the starting alphabets or punctuation marks from the searchValue
.
When the characters in searchValue
are not an exact match with the starting position of the given string, the string.startsWith()
returns false
. To check the resulting boolean value, we passed a different searchValue
in the following example.
let string = "Welcome to 'coding'.";
let searchString = string.startsWith("'cod", 11);
let refString = string.startsWith("welcome", 0);
console.log(searchString);
console.log(refString);
Output:
true
false
The string.startsWith()
method checks the string or substring starts with specific characters. With the method, the users determine whether or not a particular index position of a string begins with certain characters.