This short tutorial explains JavaScript's Array map method. It covers how map turns one array into a new array, compares it with an imperative for loop, mentions common uses in React and with Promise.all, and warns against using map when the new array isn't needed. All of the main technical claims match MDN and React documentation. Only minor simplifications were found.
Claim-by-claim breakdown
Accurate (97% confidence): Array map creates a new array by calling a function on every element of the original array. MDN says map() creates a new array filled with the results of calling a provided function on every element of the calling array. Empty slots in sparse arrays are skipped, which is a minor edge case.
Accurate (96% confidence): The callback passed to map gets the current element and its index, and it should return the new value for that position. MDN lists the callback parameters as element, index and array. The callback's return value is added as a single element of the new array.
Accurate (88% confidence): Using map solves the problem without changing the original array, unlike pushing into an array inside a for loop. MDN describes map() as a copying method that does not alter 'this'. It also notes that the callback can mutate the array.
Accurate (95% confidence): React developers commonly use map to turn data arrays into JSX for the user interface. The 'Rendering Lists' page in the React docs shows how to use JavaScript's map() to turn an array of data into an array of JSX elements.
Accurate (92% confidence): You can map values to an array of promises and run them concurrently with Promise.all. MDN describes Promise.all() as taking an iterable of promises and returning one promise that fulfills when all of them fulfill. Mapping to promises and then using Promise.all is a common way to run tasks concurrently.
Accurate (95% confidence): Using map without using the array it returns is an anti-pattern. Use forEach or a regular loop instead. MDN says that because map builds a new array, calling it without using the returned array is an anti-pattern, and recommends forEach or for...of instead.