Javascript Statement Execution Order

var confirm = confirm('Are you sure?');

I just tested this statement, and I was given an error indicating that confirmit is not a function.

I immediately discovered that the variable name overwrites it. However, my question is: why?

I know that functions are first-class, and that declaring a variable with the same name as the function will rewrite it in the relative field. But my confusion comes from what I thought was a right-to-left execution, I.E. a function call is made before the destination is determined.

Is the variable defined in this case before the function call?

+4
source share
1 answer

Due to the rise of the JavaScript variable:

function myFunction() {
    // ...
    var confirm = confirm('Are you sure?');
    // ...
}

:

function myFunction() {
    var confirm;
    // ...
    confirm = confirm('Are you sure?');
    // ...
}

, :

function myFunction() {
    // ...
    var confirm = window.confirm('Are you sure?');
    // ...
}
+6

All Articles