I found the following JavaScript example here:
https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Values,_variables,_and_literals
/**
* Example 2
*/
// will return a value of undefined
var myvar = "my value";
(function() {
console.log(myvar); // undefined
var myvar = "local value";
})();
Why does console.log(myvar) return "my value" and not undefined as specified in the comment?
I tested it in Firefox and Chrome and I get the same result.
Indeed, the output is undefined.
I think it’s because myvar is redefined in the scope.
But if you comment myvar in the function scope, output will be “my value”.
Like if myvar is a global variable.
–> Try !