totn JavaScript

JavaScript: String startsWith() method

This JavaScript tutorial explains how to use the string method called startsWith() with syntax and examples.

Description

In JavaScript, startsWith() is a string method that is used to determine whether a string starts with a specific sequence of characters. Because the startsWith() method is a method of the String object, it must be invoked through a particular instance of the String class.

Syntax

In JavaScript, the syntax for the startsWith() method is:

string.startsWith(substring [, position]);

Parameters or Arguments

substring
It is the set of characters that will be searched for at the start of string.
position
Optional. It is the position within string that the search will begin. If this parameter is not provided, the startsWith() method will default to 0 and start the search at the beginning of the string.

Returns

The startsWith() method returns true if the substring is found at the start of string. Otherwise, it returns false.

Note

  • The startsWith() method performs a case-sensitive search.
  • The first character in the string is at position 0, the second character in the string is at position 1, and so on.
  • The startsWith() method does not change the value of the original string.

Example

Let's take a look at an example of how to use the startsWith() method in JavaScript.

For example:

var totn_string = 'TechOnTheNet';

console.log(totn_string.startsWith('Tech'));
console.log(totn_string.startsWith('TECH'));

In this example, we have declared a variable called totn_string that is assigned the string value of 'TechOnTheNet'. We have then invoked the startsWith() method of the totn_string variable to look for a specific set of characters at the start of totn_string.

We have written the output of the startsWith() method to the web browser console log, for demonstration purposes, to show what the startsWith() method returns.

The following will be output to the web browser console log:

true
false

As you can see, the startsWith() method returned true for the first call, but false for the second call. Because the startsWith() method performs a case-sensitive search, the substring 'Tech' is found at the start of the string 'TechOnTheNet' but 'TECH' is not found.

Specifying a Position Parameter

Next, let's see what happens if you specify a position parameter with the startsWith() method.

For example:

var totn_string = 'TechOnTheNet';

console.log(totn_string.startsWith('On',4));

The following will be output to the web browser console log:

true

In this example, we have set the position parameter to a value of 4. This means that the search will begin at position 4 in the string. So in this case, the substring 'On' is found at the start of the string 'OnTheNet' (which is position 4 until the end of the totn_string variable).

Remember that the first character in the string is at position 0, the second character in the string is at position 1, the third character in the string is at position 2, and so on.