Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre 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

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 1

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

Awesome!

Completion rate improved to 8.33

bookAccessing Elements with indexOf

Glissez pour afficher le 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

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 1
some-alt