
JavaScript: From Fundamentals to Functional JS, v2 _.map() vs .map() Functions
This course has been updated! We now recommend you take the JavaScript: From First Steps to Professional course.
Transcript from the "_.map() vs .map() Functions" Lesson
[00:00:00]
>> Little bit of a mind bender map is very similar, but there's a key difference that I want to point out, so, Here's underscore map, again, there's a native array map method, which we won't implement. It looks you could write it out just the same as you would write for each except instead of for each input map.
[00:00:23] Obviously the behavior is different because it's not each and we'll go on to that. So the key difference between each and map that people really confuse is that each does not return anything, the function does not return anything. Okay, that's really important, so even if you wanted to return something in your callback function, you can't, it doesn't work like that.
[00:00:53] Cool. Guys promise you won't forget, Okay, and then map, returns an array every time. So map, we use it to take lists and transform them into, A new array. So you can use it really to just copy an array if you want it but typically, you're going to use it to manipulate change, update it and move it around how whoever you want, so, It looks a little similar, so we take a list.
[00:01:38] And then we have a callback function. Now, we call. Each one. With this function, except that whatever this function returns is gonna go on the object. What is this function return? What is the return?
>> Just a value.
>> What value is it returning?
>> One two or three.
[00:02:10]
>> That function, so the only place it returns is in ES5 at least is where you say return. And so we're not saying return here and so what it's returning is undefined in this case, and so, We're gonna loop through and we're gonna have an array of length three of undefined values.
[00:02:36] That makes sense, why is it length three?
>> Each iteration of the original array.
>> Cuz the original array is length three, exactly. So for this utility method, it's going to return an array of the same size every time. That's how it works.