Arrays Flashcards

1
Q

I want to remove the first element from an array. This operation should modify the existing array.

How can I do this?

A

const arr = [ 1, 2, 3 ]
const element = arr.shift() //1

const arr = [ 1, 2, 3 ]
const element = arr.splice(0,1)[0]

n.b. splice returns an array and indices must be specified

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

Name this operator: ??

A

Nullish coalescing operator

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

Evaluate both statements:

const nullValue = null;

(1) nullValue ?? “Default String”;
(2) nullValue || “Default String”;

A

(1) “Default String”
(2) “Default String”

?? is a logical operator that returns its RHS operand when its LHS operand is NULL or UNDEFINED.

The logical OR operand (||) returns the RHS operand if the LHS operand is ANY FALSY VALUE, not only null or undefined.

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

Evaluate both statements:

const emptyString = “”;

(1) emptyString ?? “Default String”;
(2) emptyString || “Default String”;

A

(1) “”
(2) “Default String”

?? is a logical operator that returns its RHS operand when its LHS operand is NULL or UNDEFINED.

The logical OR operand (||) returns the RHS operand if the LHS operand is ANY FALSY VALUE, not only null or undefined.

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

Rewrite the following using the optional chaining operator:

obj.first && obj.first.second

A

obj.first?.second

By using the ?. operator instead of just ., JavaScript knows to implicitly check to be sure obj.first is not null or undefined before attempting to access obj.first.second.

If obj.first is null or undefined, the expression automatically short-circuits, returning undefined.

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

I want to create a new array from two existing arrays with the second array elements coming after the first array elements. I do not want the existing arrays to be modified.

How can I do this?

A

let arr1 = [0, 1, 2];
let arr2 = [3, 4, 5];

//  Append all items from arr2 onto arr1
const arr3 = arr1.concat(arr2);

let arr1 = [0, 1, 2];
let arr2 = [3, 4, 5];

const arr3 = […arr1, …arr2];

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

I want to update an existing array array by adding the elements from a second array. These elements should be appended to the existing array.

How can I do this?

A

let arr1 = [0, 1, 2];
let arr2 = [3, 4, 5];

arr1.push( …arr2);

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

Consider

const array1 = [1, 2, 3];
const x = array1.unshift(4, 5));

What is x?
What does array1 look like?

A

x = 5 i.e. the length of the modified array

array1 = [4, 5, 1, 2, 3]

The 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
9
Q

What is the difference between splice and slice?

A

Splice changes the contents of an existing array.
Splice returns an array containing any deleted/replaced elements. (could be empty)
Splice can be used to remove, replace or add elements in place to an array.

Slice does not change the existing array: it makes a shallow copy of the array.
Slice returns the (portion of the) copied array.
Slice copies the array returning the portion between a start and end index.

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

What is the return value (‘result’) from the following operation and what is the value of arr after the operation?

let arr = [ 1, 2, 3, 4, 5];
let result = arr.splice(3, 1, "Hello");
A

Return value = [ 4 ] (array of replaced items)

Underlying array is modified i.e. arr = [ 1, 2, 3, “Hello”, 5];

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

What is the return value (‘result’) from the following operation and what is the value of the arr after the operation?

let arr = [ 1, 2, 3, 4, 5];
let result = arr.splice(2, 4);
A

Return value = [3, 4];

Underlying array is not modified i.e. arr = [1, 2, 3, 4, 5];

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