Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Accessing Elements with indexOf | Getting Started with Array Basics
JavaScript Array Methods

bookAccessing Elements with indexOf

When working with arrays in JavaScript, you often need to find out if a certain value exists and, if so, where it is located. Searching for values in arrays is a common task, especially when handling lists of data such as user names, product IDs, or any collection of items. The indexOf method is a fundamental tool for this purpose. It allows you to search for a specific element and returns its position within the array, making it easier to work with dynamic data and perform checks or updates based on the presence of values.

1234567
const fruits = ["apple", "banana", "cherry", "date"]; const position = fruits.indexOf("cherry"); console.log(position); // 2 const missingPosition = fruits.indexOf("orange"); console.log(missingPosition); // -1
copy

In this example, you use indexOf to search for "cherry" in the fruits array. The method returns 2, which is the index where "cherry" is found. When searching for "orange", which is not in the array, indexOf returns -1. This behavior makes it easy to check if a value exists and to act accordingly.

question mark

Which statement about the indexOf method is correct?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 1

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Awesome!

Completion rate improved to 8.33

bookAccessing Elements with indexOf

Scorri per mostrare il menu

When working with arrays in JavaScript, you often need to find out if a certain value exists and, if so, where it is located. Searching for values in arrays is a common task, especially when handling lists of data such as user names, product IDs, or any collection of items. The indexOf method is a fundamental tool for this purpose. It allows you to search for a specific element and returns its position within the array, making it easier to work with dynamic data and perform checks or updates based on the presence of values.

1234567
const fruits = ["apple", "banana", "cherry", "date"]; const position = fruits.indexOf("cherry"); console.log(position); // 2 const missingPosition = fruits.indexOf("orange"); console.log(missingPosition); // -1
copy

In this example, you use indexOf to search for "cherry" in the fruits array. The method returns 2, which is the index where "cherry" is found. When searching for "orange", which is not in the array, indexOf returns -1. This behavior makes it easy to check if a value exists and to act accordingly.

question mark

Which statement about the indexOf method is correct?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 1
some-alt