| by Arround The Web | No comments

What is the Alternative to the “startsWith()” Method in JavaScript?

The “startWith()” method of JavaScript is applied to the selected string to check whether the string has started using a specific word or not. It returns the answer in “True” or” False” that describes the result. The result will be “True”, if the word has been found at the start and “False” in the case of not found. Its time complexity is equal to the number of characters to be checked in the string. To get more detailed information related to the “startWith()” method and its implementation process in JavaScript, navigate these articles links “link 1” and “link 2”.

This article will demonstrate the alternative methods of “startsWith()” method in JavaScript.

What is the Alternative to the “startsWith()” Method in JavaScript?

The “startsWith()” method does not properly work if the browser does not support ECMAScript or ES2015. In addition, web browsers like “Safari”, “Internet Explorer”, “Edge” and some other browsers do not support the “startsWith()” method. To overcome these issues, there are several alternatives that can be utilized that perform the same functionality with minimal restrictions and some of them are listed below:

Let’s discuss each of the above methods one by one.

Method 1: “indexOf()” Method Alternative of “startWith()” Method

The “indexOf()” method returns the position of the specified word or values inside the string. It stops searching the remaining part of the string once it founds the selected value in first occurrence and if the value is not found, then this method returns “-1”. So, if this method returns “0” the programmer can assume that the string starts from the specified value, as shown below:

<script>
 const provString = 'Welcome to Linuxhint Article!';
 const startWord = 'Welcome';
 
  if (provString.indexOf(startWord) === 0) {
   console.log('String is Starting From Provided "' + startWord + '" word.');
  }
</script>

 
In the above code:

    • First, the two variables are declared one contains the string and the other contains the word or value whose position in the string needs to be checked.
    • Next, the “indexOf()” method gets applied to a variable and the value variable is passed as a parameter.
    • The result of the above method is compared with the “0” using the “if” statement and if the comparison results return true, the success message gets displayed on the console.

After the compilation of the above code block, the console looks like this:


The output shows that the value “Welcome” is residing at the start of a string.

Method 2: “lastIndexOf()” Method Alternative of “startWith()” Method

The “lastIndexOf()” method can be modified by starting the execution from the “0” index or from the start of the string to check if the specified substring resides at the start or not, as shown below:

<script type="text/javascript">
 const provString = 'Welcome to Linuxhint Article!';
 const startWord = 'Welcome';

 if (provString.lastIndexOf(startWord, 0) === 0) {
  console.log('String is Started From Provided Word');
 }
</script>

 
Description of the above code:

    • First, two variables are created. The first variable contains the string and the second variable stores the word which is going to be checked.
    • Next, the “lastIndexOf()” method is called along with the variable containing string. The method passes two parameter, one is a variable that contain the word and second it index number that is “0”.
    • The result of the above method is compared with the “0” using the “if” statement. In the case of the “true” result, the success message gets displayed.

After the end of the compilation phase, the console appears like this:


The output shows that the specified word exists at the start of a string.

Method 3: “slice()” Method Alternative of “startWith()” Method

The “slice()” method has the functionality to extract a specific part from the provided string according to the provided index number. For extracting the first word from the string, the starting index is set to “0” to begin slicing from the start, and the ending index is set to the length of the substring which is going to be checked, as shown below:

<script>
 const provString = 'Welcome to Linuxhint Article!';
 const startWord = 'Welcome';
 if (provString.slice(0, startWord.length) === startWord) {
  console.log( searchText + " is residing at the start of the string")
 }
</script>

 
Compilation of the above code:

    • First, define two variables named “provString” and “startWord” and assign them string and substring which is going to be checked as the first word of the string.
    • Next, the starting index of “0” and the final index of “startWord.length” which basically calculates the lengths of the substring are passed as the parameters of the “slice()” method. This method is then attached to the “provString” and its result is checked with the “startWord” variable using the “if” statement.
    • Also, display the success message according to the returned value of the “if” statement.

After the compilation, the console looks like this:


The output shows that the selected substring is residing at the beginning of the string.

Method 4: “substring()” Method Alternative of “startWith()” Method

The “substring()” method works similarly to the “slice()” method as it also accepts two parameters as starting and ending number and extract the portion of provided string residing inside the range. This method can be used to extract only the first word from the string and then match the extracted word with the provided word or value:

<script>
 const provString = 'Welcome to Linuxhint Article!';
 const startWord = 'Linuxhint';

 if (provString.substring(0, startWord.length) === startWord) {
  console.log("String starts with Provided Word : " + startWord)
 }else{
  console.log("String is not Starting from Provided Word: " + startWord)
 }
</script>

 
Explanation of the above code:

    • First, define two variables “provString” and “startWord” in the same manner and provide the starting and ending indexes to the “substring()” method.
    • Next, match the resultant output from the provided word using the “if” statement and display the success or error messages on the console accordingly.

After the compilation:


The output shows that provided string is not placed at the beginning of the string.

Conclusion

There are four alternatives for “startsWith()” method, these are “indexOf()”, “lastIndexOf()”, “slice()”, and “substring()” method. The “indexOf()” property starts at the beginning and stops when it founds the required word’s in the string. The “lastIndexOf()” works similarly, just an initial index of “0” needs to be provided to start the execution. The “slice()” and “substring()” extract the first word from the string and match it with the provided word or value to ensure that the provided elements exist at the start of the string or not. This blog explains the alternative methods for the “startWith()” method in JavaScript.

Share Button

Source: linuxhint.com

Leave a Reply