What is the String.startsWith method in JavaScript?

The **startsWith method **checks if a string starts with a specific other string given as a parameter.

Syntax

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.

Example

let str = "JavaScript";
console.log("The String is " + str);
console.log("check if String startwith Java: " + str.startsWith("Java"))

The startsWith method is a case sensitive method"java" is not equal to “Java”.

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.

Refer here for the string index that the following program reads.
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 case
console.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.

New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources