Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

javascript
string
startwith
communitycreator

What is the String.startsWith method in JavaScript?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code

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.

RELATED TAGS

javascript
string
startwith
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code
Keep Exploring