Silly question (JS)

This is probably a silly question. But it’s something that maybe I miss in previous lessons of the FCC JS course.

const squareList = (arr) => {
 // Only change code below this line
    var numbers = arr.filter(n => n > 0 && Number.isInteger(n) === true) 
   .map(n => Math.pow(n, 2))

return numbers;
 // Only change code above this line

My question is: Why the dot (.) before calling the map method? I already was trying to solve this challenge, but I got stuck at calling the Map method, I end up looking at the solution and what I found was that I should have just used a dot.


Hello and welcome to the forum :grin:!

Because map is a method of the Array object in JavaScript, hence you must use a dot to call it.

Maybe you’re confused by the method chaining? If so, the arr is filtered with arr.filter. Since arr.filter returns the filtered array, it still is of the array type, hence you can add another dot to chain the calls.


arr.filter(n => n > 0 && Number.isInteger(n) === true).map(n => Math.pow(n, 2));

Is, essentially, the same as this:

const filtered = arr.filter(n => n > 0 && Number.isInteger(n) === true);
const mapped = => Math.pow(n, 2));

I hope it helps :slight_smile:.

I thought about it when I saw the dot. But then I did exactly what you did.

Well, the difference is that for some reason I didn’t create a new variable to store the result of the map operation. I tried to call the method using the same variable than .filter.


Thank you, it actually helped to me understand. And thank you for the welcome!

1 Like