JavaScript: Arrays Flashcards
What is an array used for?
What is the syntax?
Arrays are used to store groups of data in a single variable.
const names = [“Andre”, “Karl”, “Rashida”, “Olivia”];
How can an entire array can be accessed?
What is the syntax?
The entire array can be accessed by using the array’s name.
console.log(names);
What does it mean that arrays are zero-indexed?
Arrays are zero-indexed, so the index of first element in the array is 0.
console.log(names[0]);
How do you log a single element from an array?
What is the syntax?
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 do you use index to replace data in an array?
What is the syntax?
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”;
What is the syntax to log data from an array along with a phrase (string)?
Ex. The fourth name is [data-from-array].
const names = [“Andre”, “Karl”, “Rashida”, “Olivia”]
names[3] = “Carter”;
…
Logs “The fourth name is Carter.”
console.log(‘The fourth name is ${names[3]}.’);
How can you determine the length of an array?
What is the syntax?
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}.`);