Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele What Are Arrays? | Section
JavaScript Essentials for Beginners - 1768407374405

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?

Select the correct answer

question mark

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

Select the correct answer

question mark

What does the following code output?

Select the correct answer

question mark

What will be the output of the following code?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 56

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

bookWhat Are Arrays?

Pyyhkäise näyttääksesi valikon

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?

Select the correct answer

question mark

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

Select the correct answer

question mark

What does the following code output?

Select the correct answer

question mark

What will be the output of the following code?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 56
some-alt