Arrays & Basic Array Operations (methods) Flashcards

1
Q

What is an array in JS?

A

A data structure for storing values of different types.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
const friends = [ 'Micheal', 'Steve', 'Peter' ];
 What other way can an array be created in JS apart from the literal syntax above?
A

const friends = new Array (‘Micheal’, ‘Steve’, ‘Peter’ );

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

How are elements in an array accessed?

A

Elements in an array are accessed using their indexes.

Example:
console.log(friends[0]);

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

How do you get the length of an array?

A

The length of an array can be gotten by using the Array.length property.

Example:- console.log(friends.length);

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

How do you access the last element in an array?

A

console.log(friends[friends.length-1]);

//This is because the .length property is not zero based while arrays are zero based, hence the length of the array - 1 will give the last item in the array.

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

Explain the JavaScript Array.push() Method.

A

The Array.push() method adds a new item or new items to the end of an array and returns the new length.

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

Explain the JavaScript Array.unshift() Method.

A

The Array.unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.

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

Explain the JavaScript Array.pop() Method.

A

The Array.pop() method removes the last element from an array and returns that element.

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

Explain the JavaScript Array.shift() Method.

A

The Array.shift() method removes the first element from an array and returns that removed element.

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

Explain the JavaScript Array.indexOf() Method.

A

The Array.indexOf() method returns the first index at which a given element can be found in the array or -1 if it is not present.

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

Explain the JavaScript Array.includes() Method.

A

The Array.includes() method determines whether an array includes a certain value among it entries , returning true or false as appropriate.

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