The startsWith()
method in TypeScript checks if a string starts with another specified string. If this is true, then a true
is returned. Otherwise, false
is returned.
string.startsWith(anotherString)
anotherString
: This is the string we want to check to see if string
ends with it.
The startsWith()
method returns true
if the specified string anotherString
starts the input string. Otherwise, false
is returned.
export {}// create some stringslet greeting:string = "Welcome to Edpresso"let proverb:string = "Make hay while the sun shines"let author:string = "Chinua Achebe"let book:string = "Things fall apart"// check if some substrings end themconsole.log(greeting.startsWith("Welcome")) // trueconsole.log(proverb.startsWith("Make hay")) // trueconsole.log(author.startsWith("Achebe")) // falseconsole.log(book.startsWith("fall")) // false
Let's see what's happening in the code above: