JavaScript(JS) string method - startswith

The startsWith() method is a string method in JavaScript that is used to determine whether a string starts with a specified substring.

Here is the syntax for the startsWith() method:

str.startsWith(searchString, position)
Sou‮cr‬e:www.theitroad.com

Here, str is the string you want to search in, searchString is the substring you want to search for, and position is an optional parameter that specifies the position in the str where the search should start. If not specified, position is assumed to be 0.

The startsWith() method returns a boolean value indicating whether or not the str starts with the specified searchString.

Here is an example of using the startsWith() method:

let str = "hello world";
let result = str.startsWith("hello");
console.log(result); // true

In the example above, the startsWith() method checks whether the str starts with the substring "hello". Since the str starts with "hello", the method returns true.

If the position is specified, the startsWith() method will start searching from that position in the str.

Here is an example of using the startsWith() method with a specified position:

let str = "hello world";
let result = str.startsWith("world", 6);
console.log(result); // true

In the example above, the startsWith() method checks whether the substring "world" is at position 6 or later in the str. Since the str starts with "world" at position 6, the method returns true.