Home > Web Front-end > Front-end Q&A > How to express javascript array subscript

How to express javascript array subscript

PHPz
Release: 2023-04-25 14:48:36
Original
1321 people have browsed it

In JavaScript, an array subscript is an integer index starting from 0 and used to access elements in the array. Array subscripts can be represented using square bracket notation.

For example, suppose we create an array and assign the value:

let fruits = ['apple', 'banana', 'orange', 'grape'];
Copy after login

We can access a specific element by using the index of the array. For example, to access the first element in the array (i.e. "apple"), we can use index 0 as shown below:

console.log(fruits[0]); // 输出 "apple"
Copy after login

The same method can be used to access other elements. For example, to access the third element in the array (i.e. "orange"), we can use index 2 as shown below:

console.log(fruits[2]); // 输出 "orange"
Copy after login

In JavaScript, arrays can also be iterated using a for loop. We can use the length property of the array to determine the conditions for the iteration to terminate, as follows:

for (let i = 0; i < fruits.length; i++) {
  console.log(fruits[i]);
}
Copy after login

This will output the elements in the array one by one:

apple
banana
orange
grape
Copy after login

In short, array subscripts are used in JavaScript For accessing elements in an array, square brackets are used, starting from 0 and ending with the array length - 1.

The above is the detailed content of How to express javascript array subscript. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template