Yogesh Chauhan's Blog

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!


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 add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
3 Common Usability Mistakes In Web DesignUI/UX@mixin and @include in SCSS (Sass)SCSSHow to show confirmation alerts with OK and cancel buttons using Swift 5?SwiftWhat’s the difference between a Framework and a Library?MiscellaneousWordPress: How to display fields from ACF Flexible Contents?WordPressHow to apply style to the first-child using jQuery?jQuery