DEV Community

Phạm Đức Huy
Phạm Đức Huy

Posted on

Answer: Why a variable defined global is undefined? [duplicate]

You have just stumbled on a js "feature" called hoisting

var myname = "global"; // global variable
function func() {
    alert(myname); // "undefined"
    var myname = "local";
    alert(myname); // "local"

In this code when you define func the compiler looks at the function body. It sees that you…

Discussion (0)