It takes two parameters:
- The string against which the original string is checked.
- The index from which you want to start the searching.
Table of Contents
str.startsWith(stringToBeSearched [, startingPosition])
- True: when the specified string is found in the original string.
- False: when the specified string is not found in the original string.
Case#01: Using startsWith() without Specifying the Index Position
Note: the searching is case-sensitive.
//Case#01: When starting index is not specified var msg = "It is mostly hot in April "; var result = msg.startsWith("It"); console.log(result); result = msg.startsWith("hot"); console.log(result);
Case#02: Using startsWith() with the Index Position
//Case#02: When starting index is specified var msg = "It is mostly hot in April"; var result = msg.startsWith("April", 20); console.log(result); result = msg.startsWith("hot", 23); console.log(result);
In this short tutorial, we discussed the String.prototype.startsWith() method and how it can be used to manipulate a string object. It basically checks for a string within another string with the help of two parameters, out of which one is mandatory, and the other one is optional. It returns a Boolean value to the user indicating the existence of the substring within the string.