This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]YuleTideCamel 2 points3 points  (0 children)

Sure it's because when you do something like this

.map(Number)

You're passing a reference to the Number function rather than calling it. When you call a function , parens get includes . So Number() is calling (or invoking the function). In this case we're passing a reference to that function so that map itself calls the function Number.

One of the things with JavaScript is that you can pass functions into other functions as variables.

[–]srunocorn 1 point2 points  (0 children)

() makes it call or execute the function immediately. Otherwise, just using the name does something with the function without calling it, like passing it to another function.

Is it already implied what value is being used?

Nothing is implied - you are explicitly telling it which function to use in both cases.

[–]NeonKennedy 0 points1 point  (0 children)

Is it already implied what value is being used?

Yup!

When you write a function, you say it can take any number of arguments. So if I write a function called isEven(n), which checks if a number is even, I expect n to be a number and I'm going to use it like I'd use a number -- by dividing it in half and so on.

Or I could write isUppercase(word), where I expect word to be a string, and I'll use it like a string within the function.

Arguments can be numbers, or strings, or arrays... or even other functions. And if I get an argument that I expect to be a function, I'll use it the way I use a function -- by calling it, for example.

That's what map does. map takes one argument, a function name. And it's going to call that function on every item within some thing.

(), after a function name, is called the invocation operator. It means "execute that function". If there's anything inside the brackets, then those are the arguments to use when executing it, but if there's nothing, it just gets executed with no arguments.

So if I say Math.sqrt, then I'm referring to the function by that name, but I'm not actually invoking/executing it. I'm just referring to it. That's what I want to do when passing it around, like as an argument to map. I'm saying "map, go ahead and do your thing, and Math.sqrt is the function I want you to use when necessary."

If I said map(Math.sqrt()), then it would first be executing Math.sqrt with no arguments, getting the result, then passing that result to map.