Using String startsWith() method to coding in Javascript/JS

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

Description

In JavaScript, startsWith() is a string technique that is used to decide whether a string starts with a precise sequence of characters. Because the startsWith() technique is a method of the String object, it ought to be invoked thru a unique occasion 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 begin of string. position Optional. It is the function within string that the search will begin. If this parameter is not provided, the startsWith() approach will default to zero and begin the search at the beginning of the string.

Returns

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

Note

The startsWith() approach performs a case-sensitive search. The first persona in the string is at function 0, the 2nd persona in the string is at role 1, and so on. The startsWith() technique does not trade the price of the unique string.

Example

Let’s take a appear at an instance 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 fee of ‘TechOnTheNet’. We have then invoked the startsWith() technique of the totn_string variable to look for a unique set of characters at the start of totn_string.

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

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

true
false

As you can see, the startsWith() approach lower back actual for the first call, but false for the 2d call. Because the startsWith() approach performs a case-sensitive search, the substring ‘Tech’ is determined at the begin of the string ‘TechOnTheNet’ but ‘TECH’ is now 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 internet browser console log:

true

In this example, we have set the function parameter to a price of four This potential that the search will start at role four in the string. So in this case, the substring ‘On’ is determined at the begin of the string ‘OnTheNet’ (which is position 4 till the stop of the totn_string variable).

Remember that the first character in the string is at function 0, the 2d personality in the string is at function 1, the 1/3 persona in the string is at function 2, and so on.