Selected Reading

JavaScript String startsWith() Method



The JavaScript String startsWith() method is used to determine whether the string begins with the specified string. It returns a boolean value 'true' if the specified string characters are found at the beginning of the string; otherwise, it returns 'false'.

This method throws a TypeError exception if the searchString parameter is a regular expression. It is case sensitive, so it treats "Hello" and "hello" as different strings.

Syntax

Following is the syntax of JavaScript String startsWith() method −

startsWith(searchString, position)

Parameters

This method accepts two parameters: 'searchString' and 'position'. The position parameter is optional and specifies the position within the string where the search starts.

  • searchString −The characters to be searched at the start of this string.
  • position (optional) − The start position at which the searchString expected to be found.

Return value

This method returns 'true', if string begins with sepcified string; 'false' otherwise.

Example 1

If the string starts with the specified characters, it returns true.

In this example, we use the JavaScript String startsWith() method to check whether the string "TutorialsPoint" starts with the specified substring "Tuto".

<html><head><title>JavaScript String startsWith() Method</title></head><body><script>   const str = "TutorialsPoint";   const searchString = "Tuto";   document.write("Original string: ", str);   document.write("<br>Search string: ", searchString);   document.write("<br>Is string '", str, "' begins with '", searchString, "' or not? ", str.startsWith(searchString));</script></body></html>

Output

The above program returns 'true'.

Original string: TutorialsPointSearch string: TutoIs string 'TutorialsPoint' begins with 'Tuto' or not? true

Example 2

If the string does not start with the specified characters, it returns false.

Here is another example of the JavaScript String startsWith() method. In this example, we use this method to determine whether the string "Hello World" begins with the specified string "hello".

<html><head><title>JavaScript String startsWith() Method</title></head><body><script>   const str = "Hello World";   const searchString = "hello";   document.write("Original string: ", str);   document.write("<br>Search string: ", searchString);   document.write("<br>Is string '", str, "' begins with '", searchString, "' or not? ", str.startsWith(searchString));</script></body></html>

Output

After executing the above program, it returns 'false'.

Original string: Hello WorldSearch string: helloIs string 'Hello World' begins with 'hello' or not? false

Example 3

If we pass a position parameter value as 5, it checks if the string starting from index(position) 5 begins with the specified substring. If the substring matches the beginning of the new string (from index 5), it returns 'true'; otherwise, it returns 'false'.

<html><head><title>JavaScript String startsWith() Method</title></head><body><script>   const str = "Welcome to Tutorials Point";   let endPosition = 5;   const searchString = "me";   document.write("Original string: ", str);   document.write("<br>End position: ", endPosition);   document.write("<br>Search string: ", searchString);   document.write("<br>String actual length: ", str.length);   document.write("<br>Is string '", str, "'(start at length 5) begins with '",searchString,"' or not? ", str.startsWith(searchString, endPosition));</script></body></html>

Output

Once the above program is executed, it will return 'true'.

Original string: Welcome to Tutorials PointEnd position: 5Search string: meString actual length: 26Is string 'Welcome to Tutorials Point'(start at length 5) begins with 'me' or not? true

Example 4

The JavaScript String startsWith() method expects the searchString parameter to be a string. If it is a regular expression, it throws a 'TypeError' exception.

<html><head><title>JavaScript String startsWith() Method</title></head><body><script>   const str = "Tutorials Point";   const searchString = /ab+c/;   document.write("Original string: ", str);   document.write("<br>Search string: ", searchString);   try {      document.write("Is string '",str,"' is begins with '",searchString,"' or not? ", str.startsWith(searchString));   } catch (error) {      document.write("<br>", error);   }</script></body></html>

Output

The above program thorws a 'RangeError' exception.

Original string: Tutorials PointSearch string: /ab+c/TypeError: First argument to String.prototype.startsWith must not be a regular expression
Advertisements