Arrays Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What is an array?

A

An object that lets us store many values under one variable name, in the form of an ordered list

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

With what keyword should we declare an array? Why?

A

const

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

What are 3 different ways to create and express an array?

A

(1) const name = [item1, item2 …];

can also format as:
const name = [
item1,
item2,

];

(2) let name = new Array();

(3) define an empty array, then assign values to each index

const cars = [];
cars[0]= “Saab”;
cars[1]= “Volvo”;
cars[2]= “BMW”;

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

When using the new in-built array constructor, what’s something we should be careful of?

A

If we use new for one single value that is a number e.g. new Array(40), it will try to create 40 items instead of a single item with the value of 40

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

How do you access an array - esp the first and last element?

A

array[index number]

for first element: array[0]
for last element: array[array.length - 1]

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

How do you change an array element?

A

array[index] = new value

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

Identify two ways to add array elements

A

(1) .push()
e.g. array.push(new element)
(2) .length
e.g. fruits[fruits.length] ="Lemon";

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

How can we make sure an element is an array?

A

Use Array.isArray() method
Using typeof will return an object, because an array is a type of object

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