JavaScript: Arrays Flashcards

1
Q

What is an array used for?
What is the syntax?

A

Arrays are used to store groups of data in a single variable.

const names = [“Andre”, “Karl”, “Rashida”, “Olivia”];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How can an entire array can be accessed?
What is the syntax?

A

The entire array can be accessed by using the array’s name.

console.log(names);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does it mean that arrays are zero-indexed?

A

Arrays are zero-indexed, so the index of first element in the array is 0.

console.log(names[0]);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you log a single element from an array?
What is the syntax?

A

const names = [“Andre”, “Karl”, “Rashida”, “Olivia”]

To log a single element, we use the name of the array with the index in brackets.

Returns “Karl” -
console.log(names[1]);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you use index to replace data in an array?
What is the syntax?

A

const names = [“Andre”, “Karl”, “Rashida”, “Olivia”]

We can also use index to replace data in an array.

Returns “Olivia” -
console.log(names[3]);

Replaces “Olivia” with “Carter” -
names[3] = “Carter”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the syntax to log data from an array along with a phrase (string)?

Ex. The fourth name is [data-from-array].

A

const names = [“Andre”, “Karl”, “Rashida”, “Olivia”]
names[3] = “Carter”;

Logs “The fourth name is Carter.”

console.log(‘The fourth name is ${names[3]}.’);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How can you determine the length of an array?
What is the syntax?

A

We use the array’s “length property” to determine how many elements are in the array.

arrayname.length

console.log(‘The length of the array is ${names.length}.`);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly