Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

jquery
arrays
index
communitycreator

What is the inArray() method in jQuery?

Shubham Singh Kshatriya

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The _.inArray() method in jQuery is used to get the index of a specific value from an array.

Syntax

_.inArray(value, array, startIndex)
Syntax for the _.inArray() method

Parameters

This method accepts the following parameters.

  • value: This is the value to be searched.
  • array: This is the array to be queried.
  • startIndex: This is the index from where the search starts. If not specified, it will take 0 as the default starting index.

Return value

This method returns the index of the specified value if found. Otherwise, it returns -1.

Example

Console
Implementation of the _.inArray() method

Explanation

In the HTML tab, we do the following:

  • Line 5: We import the jQuery script.

In the JavaScript tab, we do the following:

  • Line 2: We create an array, numbers, and populate it with a few values.
  • Line 5: We declare a variable, value, and initialize it with 3.
  • Line 8: We use the _.inArray() method to find the index of value in numbers and store it in the variable, index.
  • Line 11: We print index on the console.

RELATED TAGS

jquery
arrays
index
communitycreator

CONTRIBUTOR

Shubham Singh Kshatriya

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring