JavaScript startsWith() Method

JavaScript startsWith() Method | To verify if a string begins with a letter or a certain string, we can use the JavaScript startsWith() function. In the event that the string begins with the provided characters, the startsWith() method outputs a true boolean expression.

To determine whether the inputted string comprises a substring, this method is frequently used. Although there are other ways to locate substrings, the startsWith() technique is required to evaluate a string’s beginning.

JavaScript String startsWith() Method

Syntax:-
startsWith(searchString: string, position?: number): boolean;
It returns true if the sequence of elements of searchString converted to a String is the same as the corresponding elements of this object (converted to a String) starting at position. Otherwise returns false.

startsWith() Parameters,
The startsWith method accepts 2 parameters:-

  1. Search Value
  2. Starting Index

JavaScript startsWith() Example

const str = 'Know Program';
console.log(str.startsWith('Know'));
console.log(str.startsWith('Know', 3));
console.log(str.startsWith('Program', 5));

Output:-

true
false
true

The first line in the code given yields true since the string starts with “Know” Furthermore, we have provided a position parameter in the second statement. Therefore the startsWith() method outputs a false after beginning its search from the 3 keys. The last line also yields a true result because “Program” begins in the fifth index.

startsWith() JavaScript Example with only Search Value

In the startsWith() method, the search value field must be filled out. We enter the required search value here. A single character or more may be used for this. Here are some illustrations:

const person = 'John Doe';
console.log(person.startsWith('J'));
console.log(person.startsWith('D'));

Output:-

true
false

startsWith() method JavaScript Example with Multiple Characters

const person = 'John Doe';
console.log(person.startsWith('John'));
console.log(person.startsWith('Doe'));

Output:-

true
false

JavaScript startsWith() Method Example with Multiple Words

const person = 'John Doe';
console.log(person.startsWith('John D'));
console.log(person.startsWith('ohn Doe'));

Output:-

true
false

JavaScript String startsWith() Method Example Beyond the Length of the String

If the given search value string has a length greater than the original string then the startsWith() method of String in JavaScript will always return false.

const person = 'John Doe';
console.log(person.startsWith('John Doe excels in computer science'));

Output:-

false

JavaScript startsWith() Method Example with Initial Index

const person = 'John Doe';
console.log(person.startsWith('J', 0));
console.log(person.startsWith('J', 5));
console.log(person.startsWith('D', 0));
console.log(person.startsWith('D', 5));

Output:-

true
false
false
true

startsWith() JavaScript Example with Negative at Initial Index

A negative index, hence won’t work. If we attempt to be smart by testing whether a negative index would function similarly to slice(), where the last character would be returned if a negative index is passed. 

const person = 'John Doe';
console.log(person.startsWith('J', -1));
console.log(person.startsWith('o', -2));
console.log(person.startsWith('e', -1));
console.log(person.startsWith('D', -1));

Output:-

true
false
false
false

The startsWith technique should be used with caution because it is case-sensitive. Unlike JavaScript’s includes() method, the startsWith method is used only to determine whether a string begins with another string.

However, we would advise utilizing the includes() technique if you are only trying to identify a substring. You can try the endsWith() method after you have finished training with the startsWith() approaches.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *