What is startsWith() Method in JavaScript ?
In JavaScript, the startsWith()
method is used to determine whether a string starts with a specified substring. It returns true
if the string begins with the characters of the specified substring, and false
otherwise.
Syntax:
string.startsWith(searchString, position)
string
: The string to be searched.searchString
: The substring to search for at the beginning ofstring
.position
(optional): The position within the string at which to begin the search. Default is0
.
Example: Here, the startsWith()
method is called on the str
string to check if it starts with the substring “Hello”. Since “Hello” is present at the beginning of the string, the method returns true
, which is then assigned to the variable startsWithHello
.
Javascript
const str = "Hello, World!" ; const startsWithHello = str.startsWith( "Hello" ); console.log(startsWithHello); // Output: true |
Output
true
Contact Us