Arrays Flashcards

1
Q

What is an array?

A

It’s a data structure that can hold multiple values of different data types.

Arrays are created using square brackets [] and can be populated with values separated by commas.

Each value in an array is assigned an index starting from 0.

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

How do you access elements in an array?

A

Use a square bracket notation [].

The index of the first element in the array is 0, and the index of the last element is the length of the array minus 1.

For example, myArray[0] will access the first element of the array myArray.

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

What is the length property of an array?

A

It’s a built-in property of arrays that returns the number of elements in an array.

The length property is automatically updated when elements are added to or removed from the array.

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

What is an array?

A

an ordered collection of values, identified by an index

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

Can you give an example for destructuring an object or an array?

A

Destructuring is an expression available in ES6 which enables a succinct and convenient way to extract values of Objects or Arrays and place them into distinct variables.

Array destructuring

// Variable assignment.
const foo = ['one', 'two', 'three'];

const [one, two, three] = foo;
console.log(one); // "one"
console.log(two); // "two"
console.log(three); // "three"
// Swapping variables
let a = 1;
let b = 3;

[a, b] = [b, a];
console.log(a); // 3
console.log(b); // 1

Object destructuring

// Variable assignment.
const o = {p: 42, q: true};
const {p, q} = o;

console.log(p); // 42
console.log(q); // true
How well did you know this?
1
Not at all
2
3
4
5
Perfectly