Arrays Flashcards
I want to remove the first element from an array. This operation should modify the existing array.
How can I do this?
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
Name this operator: ??
Nullish coalescing operator
Evaluate both statements:
const nullValue = null;
(1) nullValue ?? “Default String”;
(2) nullValue || “Default String”;
(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.
Evaluate both statements:
const emptyString = “”;
(1) emptyString ?? “Default String”;
(2) emptyString || “Default String”;
(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.
Rewrite the following using the optional chaining operator:
obj.first && obj.first.second
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.
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?
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];
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?
let arr1 = [0, 1, 2];
let arr2 = [3, 4, 5];
arr1.push( …arr2);
Consider
const array1 = [1, 2, 3]; const x = array1.unshift(4, 5));
What is x?
What does array1 look like?
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.
What is the difference between splice and slice?
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.
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");
Return value = [ 4 ] (array of replaced items)
Underlying array is modified i.e. arr = [ 1, 2, 3, “Hello”, 5];
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);
Return value = [3, 4];
Underlying array is not modified i.e. arr = [1, 2, 3, 4, 5];