Iterators Flashcards
There are three important things to know about the .forEach() method.
It is an array method. It must be called upon an array.
Any changes to the iterated array value won’t be updated in the original array.
The return value is undefined.
Review: Iterators
.forEach() is used to execute the same code on every element in an array but does not change the array and returns undefined.
.map() executes the same code on every element in an array and returns a new array with the updated elements.
.filter() checks every element in an array to see if it meets certain criteria and returns a new array with the elements that return truthy for the criteria.
All iterator methods can be written using arrow function syntax. In fact, given the succinctness and the implicit return of arrow function syntax, this is quickly becoming the preferred way to write these types of method calls.
You can visit the Mozilla Developer Network to learn more about iterator methods (and all other parts of JavaScript!).
Additional iterator methods such as .some(), .every(), .reduce() perform different functions.
.map()
Returns new array with contents changed per code block
.filter()
.filter() returns certain elements from the original array that evaluate to truthy based on conditions written in the block of the method.
split()
The split() method splits a String object into an array of strings by separating the string into substrings, using a specified separator string to determine where to make each split.
Which of the following methods returns a boolean value?
Which of the following methods returns a boolean value?
.some()
.filter()
.map()
.forEach()
.some()
What will the following code return?
let animals = [‘bears’, ‘cats’, ‘dogs’, ‘elephants’, ‘giraffes’];
animals.some(animal => animal.length < 5);
What will the following code return?
[‘cats’, ‘dogs’]
true
false
true
What is the correct way to refactor the code below to use arrow function syntax?
namesArray.forEach(function(name) {
console.log(‘Welcome, ‘ + name + ‘!’);
}
What is the correct way to refactor the code below to use arrow function syntax?
namesArray.forEach => console.log(‘Welcome, ‘ + name + ‘!’);
namesArray.forEach( => console.log(‘Welcome, ‘ + name + ‘!’));
namesArray.forEach(function => name {
console.log(‘Welcome, ‘ + name + ‘!’);
}
namesArray.forEach(name => console.log(‘Welcome, ‘ + name + ‘!’));
namesArray.forEach(name => console.log(‘Welcome, ‘ + name + ‘!’));
Which of the following iterator methods returns undefined?
Which of the following iterator methods returns undefined
?
.map()
.filter()
.forEach()
.some()
.forEach()
Which of the following methods returns a new array?
Which of the following methods returns a new array?
.some()
.forEach()
.every()
.filter();
.filter();
Which of the following methods returns an array with values that evaluate to truthy based on the condition in the method’s block?
Which of the following methods returns an array with values that evaluate to truthy based on the condition in the method’s block?
.forEach()
.some()
.map()
.filter()
.filter()