Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ What Are Arrays? | Section
JavaScript Fundamentals

bookWhat Are Arrays?

メニューを表示するにはスワイプしてください

Note
Definition

An array is simply an ordered collection of variables.

Defining an Array

The general syntax of defining an array is:

let arrayName = [ element1, element2, … ];

We can declare an empty array as well:

let arrayName = [];

Arrays are useful when we need to store multiple values in a single variable instead of creating separate variables for each value.

This makes data easier to organize and manipulate. For example, we can store the names of the class students in an array:

We can output all the elements of an array by simply using it's name in a console.log statement:

12345
let students = [ "Emma", "Alex", "Chris" ]; console.log(students); let emptyArray = []; console.log(emptyArray);
copy

Indexing

We can access an element at a specific position in the array by indexing.

The syntax for indexing is:

arrayName[index]

Here arrayName is the name of the array, and index refers to the position of the element in the array.

The index values start from 0, which means the first element always has an index 0.

1234
let students = [ "Emma", "Alex", "Chris" ]; console.log(students[0]); // Output: Emma console.log(students[1]); // Output: Alex console.log(students[2]); // Output: Chris
copy

Using an invalid index returns undefined:

123
let students = [ "Emma", "Alex", "Chris" ]; console.log(students[-1]); // Output: undefined console.log(students[4]); // Output: undefined
copy

1. What is an array in JavaScript?

2. Which of the following is the correct way to declare an array?

3. What does the following code output?

4. What will be the output of the following code?

question mark

What is an array in JavaScript?

正しい答えを選んでください

question mark

Which of the following is the correct way to declare an array?

正しい答えを選んでください

question mark

What does the following code output?

正しい答えを選んでください

question mark

What will be the output of the following code?

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 1.  54

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 1.  54
some-alt