-1
getWeatherData();
function getWeatherData(){
        navigator.geolocation.getCurrentPosition((success) => {
            console.log(success)
        })
    }

It returns an object with lat, long and other info, but how? Success is literally a parameter and has no value, right? It's not defined anywhere and I didn't pass a value in when I invoked the function.

Can someone explain to me how this works?

phuzi
  • 12,078
  • 3
  • 26
  • 50
  • `.getCurrentPosition()` executes the anonymous function `(success) => { ... }` and passes it the location of the user/browser (the object with lat, long, ...) to it as argument. – Andreas Jan 04 '22 at 15:22
  • Similar topic about callback parameters: [What exactly is the parameter e (event) and why pass it to JavaScript functions?](https://stackoverflow.com/q/35936365) | [Where does the 'event' param come from in javascript event handlers?](https://stackoverflow.com/q/9925947) – VLAZ Jan 04 '22 at 15:25

1 Answers1

3

Function parameters get values when the function is called.

You are passing a function (lets call it anon) as an argument to getCurrentPosition.

getCurrentPosition (which you did not write and which you are not looking at the source code of) or a function that it passes anon onto will, at some point, call it. At that point anon is passed arguments.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335