I am new to JavaScript and I was doing some practices on
local and global variable scopes, following is my code( href="http://jsfiddle.net/xjmBf/1/"
rel="noreferrer">fiddle):
class="snippet" data-lang="js" data-hide="false" data-console="true"
data-babel="false">
class="snippet-code-js lang-js prettyprint-override">
var myname =
"initial"
function c(){
alert(myname);
var myname =
"changed";
alert(myname);
}
c();
when
the first alert is called, it is showing myname
as undefined.
so my confusion is why I am not able to access a global instance of
myname
and if I don't define myname
within the function then it will work fine.
No comments:
Post a Comment