Answer would be Option 3
because of multiple of factors.
- We are using
var
here to declare the variable. - The variable
name
already exists in the global scope. - If we think in terms of hoisting then you might think answer would be
Option 1
because inside theprint
function,name
variable would be hoisted to the top and value would beundefined
. However, one thing to remember about hoisting is that it is true that variable declarations are hoisted to the top but if the variable is already initialised with a value then the value is retained. - We are capturing the variable in function arguments.
More about hoisting:
https://developer.mozilla.org/en-US/docs/Glossary/Hoisting#only_declarations_are_hoisted
More about var:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var
For more tutorials and courses, visit: https://bit.ly/devtools-yt