javascript array indexof with code examples

JavaScript Array indexOf() Method

The indexOf() method in JavaScript is a built-in function that is used to search an array for a specific element and returns its position within the array. If the element is not found, it returns -1. This method is used to determine the first occurrence of a given value in an array.

Syntax:

array.indexOf(element, start)
  • element: The element to search for in the array.
  • start (optional): The index at which to start the search. Default value is 0.

Example 1: Finding the first occurrence of an element in an array

var fruits = ["apple", "banana", "cherry", "orange"];
var pos = fruits.indexOf("cherry");
console.log(pos);  // Output: 2

Example 2: Finding the first occurrence of an element in an array after a specified index

var fruits = ["apple", "banana", "cherry", "orange", "cherry"];
var pos = fruits.indexOf("cherry", 3);
console.log(pos);  // Output: 4

Example 3: Finding a non-existing element in an array

var fruits = ["apple", "banana", "cherry", "orange"];
var pos = fruits.indexOf("grapes");
console.log(pos);  // Output: -1

Note: indexOf() is case sensitive, so it can only find elements that match the case of the search term exactly. To search for elements case-insensitively, the toLowerCase() method can be used.

var fruits = ["Apple", "Banana", "Cherry", "Orange"];
var pos = fruits.indexOf("cherry".toLowerCase());
console.log(pos);  // Output: 2

In conclusion, the indexOf() method in JavaScript is a simple and efficient way to search for an element in an array and retrieve its position. It is an important function to have in your toolkit when working with arrays.
JavaScript Array lastIndexOf() Method

The lastIndexOf() method in JavaScript is similar to the indexOf() method, but it returns the position of the last occurrence of an element in an array. If the element is not found, it returns -1.

Syntax:

array.lastIndexOf(element, start)
  • element: The element to search for in the array.
  • start (optional): The index at which to start the search from the end of the array. Default value is the array's length – 1.

Example 1: Finding the last occurrence of an element in an array

var fruits = ["apple", "banana", "cherry", "orange", "cherry"];
var pos = fruits.lastIndexOf("cherry");
console.log(pos);  // Output: 4

Example 2: Finding the last occurrence of an element in an array before a specified index

var fruits = ["apple", "banana", "cherry", "orange", "cherry"];
var pos = fruits.lastIndexOf("cherry", 3);
console.log(pos);  // Output: 2

JavaScript Array includes() Method

The includes() method in JavaScript is another built-in function that is used to determine if an array includes a specific element. It returns a boolean value of true if the element is found in the array and false if it is not.

Syntax:

array.includes(element, start)
  • element: The element to search for in the array.
  • start (optional): The index at which to start the search. Default value is 0.

Example 1: Checking if an array includes an element

var fruits = ["apple", "banana", "cherry", "orange"];
var found = fruits.includes("cherry");
console.log(found);  // Output: true

Example 2: Checking if an array does not include an element

var fruits = ["apple", "banana", "cherry", "orange"];
var found = fruits.includes("grapes");
console.log(found);  // Output: false

In conclusion, the indexOf(), lastIndexOf(), and includes() methods in JavaScript are useful functions for searching and checking the presence of elements in an array. They can be used in various situations to perform different operations with arrays.

Popular questions

  1. What is the indexOf() method in JavaScript and what is it used for?

Answer: The indexOf() method in JavaScript is a built-in function that is used to search an array for a specific element and returns its position within the array. If the element is not found, it returns -1. This method is used to determine the first occurrence of a given value in an array.

  1. What is the syntax of the indexOf() method?

Answer: The syntax of the indexOf() method is as follows:

array.indexOf(element, start)
  • element: The element to search for in the array.
  • start (optional): The index at which to start the search. Default value is 0.
  1. What is the difference between indexOf() and lastIndexOf() methods in JavaScript?

Answer: The indexOf() method in JavaScript is used to search for the first occurrence of an element in an array, while the lastIndexOf() method is used to search for the last occurrence of an element in an array.

  1. What is the includes() method in JavaScript and what is it used for?

Answer: The includes() method in JavaScript is another built-in function that is used to determine if an array includes a specific element. It returns a boolean value of true if the element is found in the array and false if it is not.

  1. What is the syntax of the includes() method?

Answer: The syntax of the includes() method is as follows:

array.includes(element, start)
  • element: The element to search for in the array.
  • start (optional): The index at which to start the search. Default value is 0.

Tag

Arrays.

Posts created 2498

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top