map specifically Flashcards

(10 cards)

1
Q

What does the map() method do in JavaScript?

A

It creates a new array by calling a provided function on every element in the original array.

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

Does map() modify the original array?

A

No, map() does not modify the original array. It returns a new array with transformed values.

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

What arguments does the callback function in map() take?

A

The callback function takes up to three arguments: currentValue, index, and the original array.

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

What is the syntax of map()?

A

array.map(function(currentValue, index, array) { /* code */ }, thisArg);

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

Give a simple example of using map().

A

const nums = [1, 2, 3]; const doubled = nums.map(n => n * 2); // [2, 4, 6]

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

How is map() different from forEach()?

A

map() returns a new array with transformed data, while forEach() just executes a function for each element and returns undefined.

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

Can you chain map() with other array methods?

A

Yes, map() is often chained with filter(), reduce(), or other methods for data transformation.

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

What happens if the callback in map() does not return anything?

A

If the callback doesn’t return a value, map() will fill the new array with undefined values.

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

Can map() be used on non-array objects?

A

Only on array-like objects that implement map(), like typed arrays or when converted using Array.from().

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

Is map() synchronous or asynchronous?

A

map() is synchronous — it doesn’t wait for async operations inside the callback to finish.

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