YogeshChauhan.com
The actual difference between indexOf() and search() in JavaScript
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!

dreamhost

Leave a Reply

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to set opacity or transparency using CSS? #3 Pagination in CSS with multiple examples #4 How to make HTML form interactive and using CSS? #5 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

Mar 3 How to embed YouTube or other video links in WordPress? Mar 3 How to change the Login Logo in WordPress? Mar 3 substring() Method in JavaScript Mar 3 Window setInterval() Method in JavaScript Mar 2 How to zoom an element on hover using CSS? Mar 2 the box-sizing property in CSS

You might also like these

How to add CurrencyPipe in TypeScript file in Angular 9 Project?AngularThe basics of CSS Box modelCSSINTERSECT and EXCEPT in PostgresPostgresUPDATE and DELETE Statements in PostgresPostgresHow to vertically and horizontally align text and image block (without flex or grid) in CSS?CSSWordPress get_posts ExamplesWordPress