JavaScript focused study deck Flashcards
What does a for…in loop return to us?
A for…in loop returns the keys of an object.
In the case of an Array, this would be the indices
This also works for strings
What does a for…of loop return to us?
A for…of loop returns each element in the iterable.
This works for strings and arrays, but not objects
What does the filter method return to us?
The filter method returns a new array with elements that match the given conditionals (the given test implemented by the given function)
What argument must we provide to a filter method?
We must provide a Callback function that runs the filtering test.
This can be a function expression, an anonymous function, or an arrow function.
Beside a callback function, what arguments can we pass to the filter method?
We can pass the index, and the array on which to run the callback
What does the map method return to us?
The map method returns a new array populated with the results of calling a provided function on each element of the original array.
What argument must we pass to the map method?
We must pass a callback function to the map method.
This callback can be a function expression, an anonymous function, or an arrow function.
What arguments, besides a callback, with the map method take?
The map method will take index as an argument, as well as the array on which to run the callback.
When shouldn’t we use the map function?
We shouldn’t use the map function when we don’t need the returned array.
In that case you should use a for…of loop or forEach
Sometimes a method might return an array with undefined, NaN, or ‘ ’ at either end based on our callback.
What can we do about that?
We can splice the returned array to remove the index value we do not want.
Does the splice method modify the array it is performed on?
Yes, it modifies the original array.
What arguments does splice take in?
The splice method takes in a starting index, a delete count, and then items to be inserted.
Delete count is optional, but must be included if there are items to be inserted
Delete count can be 0
Does the slice method alter the array it is called upon?
No, the slice method returns a shallow copy of the array in a new array
What arguments does slice take?
Slice takes the starting index (inclusive), and the end index (exclusive)
Does the sort method return a new array?
No, sort will sort the items in place, modifying the original array.