The **startsWith
method **checks if a string starts with a specific other string given as a parameter.
string.startsWith(searchString)
To search from the specific index of the string, we can use:
string.startsWith(searchString, index)
The startsWith
method will return true
if the searchString
is present in the string in which we call startsWith
; otherwise, it returns false
.
let str = "JavaScript";console.log("The String is " + str);console.log("check if String startwith Java: " + str.startsWith("Java"))
The startsWith
method
let str = "JavaScript";console.log("The String is " + str);console.log("check if String startwith java: " + str.startsWith("java"))
Let’s check if the string starts with a searchString
from a specific index.
let str = "JavaScript";console.log("The String is " + str);console.log("check if String startwith ava from the index 1: " + str.startsWith("ava", 1))console.log("check if String startwith Script from the index 4: " + str.startsWith("Script", 4))// false caseconsole.log("check if String startwith ava from the index 0: " + str.startsWith("ava", 0))
In the code above, we have checked if the string starts with “ava” from index 1, “Script” from index 4, and “ava” from index 0.