YogeshChauhan . com

The actual difference between indexOf() and search() in JavaScript

in JavaScript on July 20, 2019

The two method indexOf() and search() are same but there is a difference. Let’s talk about those methods one by one.

What the indexOf() method does is that it returns the position (index) of the first occurrence of the specified value. If that value is not found then it returns -1.

REMEMBER THIS: The indexOf() method is case sensitive.

Let’s look at the example..


var str = "Hey there, this is Yogesh Chauhan's blog. This blog has many programming articles.";
var n = str.indexOf("blog");

console.log("The first occurance of 'blog' starts at "+n);

/*Output*/
The first occurance of 'blog' starts at: 36

In the example above, the first ‘blog’ word starts at position 34 (first word position is 0 and so on). So the output displays 34 in return of indexOf() method.

Now let’s look at the search() method.

Similar to indexOf(), search() method finds a string for the specified value, and same as indexOf(), it returns the position of that value in the string.

In this method, search value can either be string or it can be a regular expression.

If that value is not found then it returns -1.

Let’s look at the example..


  var str = "Hey there, this is Yogesh Chauhan's blog. This blog has many programming articles.";
var n = str.search("blog");

console.log("The first occurance of 'blog' starts at "+n);

/*Output*/
The first occurance of 'blog' starts at: 36

DIFFERENT METHODS. SAME RESULTS. SAME RETURN VALUES.

Then why to keep both in the JavaScript?

Do they both accept same parameters or arguments, and also return the same value? —– YES

So they are same. —- NO

There are differences:

The first difference is that search() method cannot take another or second start position as parameter.

Take a look at their syntaxes.


  Syntax for search(): string.search(value to search for)

  Syntax for indexOf(): string.indexOf(value to search for, starting position)

  NOTE: The 'Starting position' is an optional field.

So, that was the first difference.

Another difference is, indexOf() method cannot take regular expressions as inputs or searching values. The regular expressions are powerful search values which can be searched only by search() method only from these two.

So those were two main differences between them. I hope that helped you guys!

amazon

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to uninstall Cocoapods from the Mac OS? #4 How to add Read More Read Less Button using JavaScript? #5 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

May 7 How to disable right click and drag and drop of images using jQuery? May 7 How to render Lists in React? May 7 What’s the difference between variables in CSS and SCSS (Sass)? May 7 How to define variables in SCSS (Sass)? May 7 How to show and hide an element on click in React? May 5 Use inline if to make a shorter conditional syntax in React

You might also like these

Flash of Invisible Text and Mitt Romney Web Font ProblemCSSHow to get Current Year, Month and Date in JavaScript?JavaScriptWhat is Conditional Rendering in React?ReactHow to Block IPs and User Agents using code in Drupal or WordPress?DrupalHow to set opacity or transparency using CSS?CSSShort-Circuit Evaluation in JavaScriptJavaScript