Javascript (Arrays) Flashcards

1
Q

What are arrays?

A

An array is a collection of elements. Arrays are JavaScript’s way of making lists. Arrays can store any data types (including strings, numbers, and booleans). Arrays are also ordered (each item has a numbered position)

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

What are array literals?

A

Array literals create an array by wrapping items in square brackets [].

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

What are the ways to create an array.

A
  1. Using array literals
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the content inside of an array called?

A

An element, code block

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

What is an index?

A

An index is a numbered position for an element. Each element in an array will have a numbered position.

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

Arrays are zero-indexed. What does this mean?

A

Zero-indexed means that the positions start counting from 0 rather than 1. Therefore, the first item in an array will be at position 0.

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

How can we update an element in an array?

A

We can update an element in an array by using the following syntax:

variableName[3] = ‘new value’;

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

What is the difference between using let and const when working with arrays?

A

When we use the let variable when working with arrays we can reassign a new array and update an element in the array. With const variables we can update an element in an array but we can not reassign a new array.

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

.length

A

The .length property is one of an array’s built in properties. It returns the number of items in the array.

Ex:
const newYearsResolutions = [‘Keep a journal’, ‘Take a falconry class’];

console.log(newYearsResolutions.length);
// Output: 2

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

.push()

A

The .push() method allows you to add items to the end of an array. The .push() changes, or mutates, the array. This method is also called a destructive array method since it changes the initial array. When we add this method to an array we can call the array like we would a function.

Ex:
const itemTracker = [‘item 0’, ‘item 1’, ‘item 2’];

itemTracker.push(‘item 3’, ‘item 4’);

console.log(itemTracker);
// Output: [‘item 0’, ‘item 1’, ‘item 2’, ‘item 3’, ‘item 4’];

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

.pop()

A

The .pop method removes the last item in an array. It does not take any arguments. This method also mutates the array. The .pop() method also returns the value of the last element. We can store this in a variable to use later in the code.

Ex:
const newItemTracker = [‘item 0’, ‘item 1’, ‘item 2’];

const removed = newItemTracker.pop();

console.log(newItemTracker);
// Output: [ ‘item 0’, ‘item 1’ ]
console.log(removed);
// Output: item 2

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

.shift()

A

The .shift method removes and returns the first element of the array. All of the elements after it will shift down one place.

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

.unshift()

A

Adds one or more elements to beginning of array and returns new length.

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

.slice()

A

The .splice() method modifies an array by inserting, removing, and/or replacing array elements then returning an array of removed elements. The splice array is a shallow copy of the original array.

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

.indexOf()

A

The .indexOf() method returns the first index at which an element can be found. Returns -1 if the element is not found.

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

What happens if we try to change an array inside a function?

A

When you pass an array into a function, if the array is mutated inside the function, that change will be maintained outside the function as well. pass-by-reference)

17
Q

What are nested arrays?

A

Nested arrays are when an array contains another array.

Ex:
const nestedArr = [[1], [2, 3]];

18
Q

How do we access nested arrays?

A

To access the nested arrays we use bracket notation with the index value.

Ex:
const nestedArr = [[1], [2, 3]];

console.log(nestedArr[1]); // Output: [2, 3]

19
Q

How can access the elements within a nested array?

A

To access the elements within a nested array we can chain, or add on, more bracket notation with index values.

Ex:
const nestedArr = [[1], [2, 3]];

console.log(nestedArr[1]); // Output: [2, 3]
console.log(nestedArr[1][0]); // Output: 2

20
Q

Array.isArray()

A

Array.isArray() checks if the passed value is an Array.

Ex:
const starbucksMenu = [‘Cafe Latte’, ‘Strawberry Acai’, ‘Pink Drink’, ‘Caramel Machiato’, ‘Pumpkin Creme Cold Brew’, ‘Dragon Drink’, ‘Pumpkin Spic Latte’];

console.log(Array.isArray(starbucksMenu));
//prints true

21
Q

.join()

A

The join() method creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string.

22
Q

at()

A

The at() method returns an indexed element from an array.
E.g. Get the third element of fruits:

const fruits = [“Banana”, “Orange”, “Apple”, “Mango”];
let index = 2;
let fruit = fruits.at(index);
Apple

23
Q

concat()

A

The concat() method concatenates (joins) two or more arrays and returns an array with the joined arrays
const arr1 = [“Cecilie”, “Lone”];
const arr2 = [“Emil”, “Tobias”, “Linus”];
const children = arr1.concat(arr2);
Cecilie,Lone,Emil,Tobias,Linus

24
Q

constructor

A

The constructor property returns the function that created the Array prototype.

For JavaScript arrays the constructor property returns:

function Array() { [native code] }

25
Q

copyWithin()

A

This method copies array elements within the array, to and from specified positions. The copyWithin() method is not supported in Internet Explorer.
const fruits = [“Banana”, “Orange”, “Apple”, “Mango”];
fruits.copyWithin(2, 0);
Banana,Orange,Banana,Orange

26
Q

every()

A

Checks if every element in an array pass a test.
Check if all values in ages[] are over 18:

const ages = [32, 33, 16, 40];

ages.every(checkAge)

function checkAge(age) {
return age > 18;
}

27
Q

Explain the simplest way to create a new array from an existing array

A

The simplest way is to use the spread operator:

const fruits = [‘banana’, ‘pear’, ‘apple’]

const morefruits = […fruits]

console.log(morefruits)
//[ ‘banana’, ‘pear’, ‘apple’ ]

Now we can modify the morefruits array without affecting in any way the original fruits array.

28
Q

Explain the simplest way to add an item to the end of an existing array

A

The simplest way is to use the push() method:

const fruits = [‘banana’, ‘pear’, ‘apple’]
fruits.push(‘mango’)
push() appends the item to the end of the array.

This method accepts multiple items.

const fruits = [‘banana’, ‘pear’, ‘apple’]
fruits.push(‘mango’, ‘orange’)

//[ ‘banana’, ‘pear’, ‘apple’, ‘mango’, ‘orange’ ]

29
Q

Explain the simplest way to add an item to the beginning of an existing array

A

In this case you can use the unshift() method:

const fruits = [‘banana’, ‘pear’, ‘apple’]
fruits.unshift(‘orange’)

fruits //[ ‘orange’, ‘banana’, ‘pear’, ‘apple’ ]

This method accepts multiple items.

const fruits = [‘banana’, ‘pear’, ‘apple’]
fruits.unshift(‘mango’, ‘orange’)

//[ ‘mango’, ‘orange’, ‘banana’, ‘pear’, ‘apple’ ]

30
Q

Name two main ways of removing an item from an array

A

We can alter the original array,
or we can create a new array without the item we want to remove.

31
Q

Name the main ways of removing items from an array while altering the original array.

A
  1. We can use pop() to remove the last item from the array,

const fruits = [‘banana’, ‘pear’, ‘apple’]
fruits.pop() //apple
fruits //[ ‘banana’, ‘pear’ ]

  1. We can use shift() to remove the first item of an array,

const fruits = [‘banana’, ‘pear’, ‘apple’]
fruits.shift() //banana
fruits //[ ‘pear’, ‘apple’ ]

  1. We can use splice() to remove elements at a specific positions in the middle of the array,

const fruits = [‘banana’, ‘pear’, ‘apple’]

//remove the item at index 2
fruits.splice(2, 1)

fruits //[ ‘banana’, ‘pear’ ]

32
Q

Give one way to create a new array without one specific item

A

If we want to create a new array without one specific item, we can first clone the array, or you can use slice().

Suppose you have an array, and you want to remove an item in position i. You can do it in this way:

const items = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’]
const i = 3

const filteredItems = items.slice(0, i-1).concat(items.slice(i, items.length))

console.log(filteredItems)
// [“a”, “b”, “d”, “e”, “f”]

slice() creates a new array with the indexes it receives. We create a new array, from start to the index we want to remove, and concatenate another array from the first position following the one we removed to the end of the array.

33
Q

Give the best way to add an item to the array without mutating its content (i.e. by creating a new array)

A

The best way to add an item to the array without mutating its content, i.e. by creating a new array, is to use the spread operator.

  1. adding a new item to the array in the first position

const fruits = [‘banana’, ‘pear’, ‘apple’]

const morefruits = [‘orange’, …fruits ]

console.log(morefruits)
//[ ‘orange’, ‘banana’, ‘pear’, ‘apple’ ]

When we use …fruits we basically expand the content of fruits. Since we enclosed that in the array literal syntax [], we’ll end up creating a new array morefruits, with the original content of fruits, plus anything we add to it.

  1. adding a new item to the array in the last position

In the same way as p.1, we can append to the end of the array using […fruits, ‘orange’]:

const fruits = [‘banana’, ‘pear’, ‘apple’]

const morefruits = […fruits, ‘orange’]

console.log(morefruits)
//[ ‘banana’, ‘pear’, ‘apple’, ‘orange’ ]

34
Q

How to initialise a new array with a set of values?

A

We can initialize a new array with a set of values using this syntax, which first initializes an array of 12 elements, and fills each element with the number 0:

const b = Array(12).fill(0)

b is now an array of 12 items that all have the value 0.

35
Q

Give one alternative way to define an array from the array literal syntax (const b = [])

A

const b = Array() is another way to define an array, which is equivalent to using the array literal syntax const b = []

36
Q

What is array destructuring?

A

Array destructuring is a way to extract variables from the items contained into an array.

Example:

const list = [1, 2]
const [first, second] = list

console.log(first) //1
console.log(second) //2

We had numbers into an array, now we have two variables, first and second, which contains the values of the 2 items of the array.

We can also destructure the array to more variables:

const list = [1, 2, 3, 4, 5]
const [first, second, third, fourth, fifth] = list

The value assigned to the variables depends on the order listed.

37
Q

Give an example of using the spread operator with array destructuring

A

const list = [1, 2, 3, 4, 5]
const [first, second, …others] = list
others is an array containing [3, 4, 5].

38
Q

Explain how we can check if an array contains a specific value

A

You can check if an array contains a value using the includes() method.

Example:

const list = [1, 2, 3, 4]

list.includes(1) //true
list.includes(5) //false

39
Q

What happens if we try to change an array inside a function?

A

When you pass an array into a function, if the array is mutated inside the function, that change will be maintained outside the function as well. pass-by-reference)