JavaScript Arrays Flashcards

1
Q

What does Property “.length” mean?

A

The “.length” property of a JavaScript array indicated the number of elements the array contains.
Example:
const numbers = [1, 2, 3, 4];

numbers.length // 4

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

What in an Index?

A

Array elements are arranged by “index” values, starting at “0” as the first element index. Elements can be accessed by their index using the array name, and the index surrounded by square brackets.

Example:
// Accessing an array element
const myArray = [100, 200, 300];

console.log(myArray[0]); // 100
console.log(myArray[1]); // 200
console.log(myArray[2]); // 300

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

What does Method “.push()” do?

A

The “.push()” method of JavaScript arrays can be used to ADD one or more elements to the END of an array. “.push()” mutates the original array and returns the new length of the array.

Example:
// Adding a single element:
const cart = [‘apple’, ‘orange’];
cart.push(‘pear’);

// Adding multiple elements:
const numbers = [1, 2];
numbers.push(3, 4, 5);

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

What does Method “.pop()” do?

A

The “.pop()” method REMOVES the LAST elements from an array and RETURNS that element.

Example:
const ingredients = [‘eggs’, ‘flour’, ‘chocolate’];

const poppedIngredient = ingredients.pop(); // ‘chocolate’
console.log(ingredients); // [‘eggs’, ‘flour’]

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

What does Mutable mean in JavaScript?

A

JavaScript arrays are “mutable”, meaning that the values they contain can be changed.
Even if they are declared using “const”, the contents can be manipulated by reassigning internal values or using methods like “.push()” and “.pop()’.

Example:
const names = [‘Alice’, ‘Bob’];

names.push(‘Carl’);
// [‘Alice’, ‘Bob’, ‘Carl’]

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

What are Arrays?

A

Arrays are lists of ordered, stored data. They can hold items that are of any data type. Arrays are created by using square brackets [ ], with individual elements separated by commas.

Example:
// An array containing numbers
const numberArray = [0, 1, 2, 3];

// An array containing different data types
const mixedArray = [1, ‘chicken’, false];

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