0
0
Javascriptprogramming~5 mins

Map method in Javascript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the map() method do in JavaScript?
The map() method creates a new array by applying a function to each element of the original array.
Click to reveal answer
beginner
How does map() differ from forEach()?
map() returns a new array with transformed elements, while forEach() just runs a function on each element without returning anything.
Click to reveal answer
intermediate
What arguments does the callback function inside map() receive?
The callback receives three arguments: the current element, the index of the element, and the original array.
Click to reveal answer
intermediate
Can map() change the size of the array it returns?
No, map() always returns a new array of the same length as the original array.
Click to reveal answer
beginner
Give an example of using map() to double numbers in an array.
Example: <br><code>const numbers = [1, 2, 3];<br>const doubled = numbers.map(n => n * 2);<br>console.log(doubled); // [2, 4, 6]</code>
Click to reveal answer
What does the map() method return?
AA single value
BUndefined
CThe original array
DA new array with transformed elements
Which of these is a valid use of map()?
ATo filter out elements from an array
BTo create a new array by modifying each element
CTo sort an array
DTo remove duplicates
What arguments does the callback function in map() receive?
AElement, index, array
BElement only
CIndex only
DArray only
If you want to change the size of an array, is map() the right method?
ANo, it keeps the same length
BYes, but only to add elements
CYes, it can add or remove elements
DNo, it deletes elements only
What will this code output? <br>[1, 2, 3].map(x => x + 1)
A[1, 3, 5]
B[1, 2, 3]
C[2, 3, 4]
D[0, 1, 2]
Explain how the map() method works and when you would use it.
Think about turning a list of raw items into a list of changed items.
You got /4 concepts.
    Describe the difference between map() and forEach() in JavaScript.
    One returns a new list, the other just runs code on each item.
    You got /4 concepts.