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!


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 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 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS? #Aug 1 What is Zdog? #Aug 1 How to add before after image effect using pure CSS? #Jul 31 Add animation to your skills bar using CSS #Jul 31 Use SwiperJS to create mobile touch sliders fast
You might also like these
PostgreSQL transactions using the BEGIN, COMMIT, and ROLLBACK statements.PostgresA Quick Comparison of JOIN and Subquery in SQLSQL/MySQLAvoid using new Array() in JavaScriptJavaScriptHow to create a placeholder loader (throbber) using CSS?CSSHow to create a link tag button using CSS?CSSHow to CREATE TABLE in SQL with and without using Another Table?SQL/MySQL