/    Sign up×
Community /Pin to ProfileBookmark

Javascript global variable and local variable

Can anyone explain the codes below better so I may learn how they work?

var g = 100;

function outer (){

var x = 99;

function inner(){

var y = 77;

}

}

Here is the description I have:

Here the code inside the inner function has access to all three variables.
The code inside outer can only access g and x, and the code outside of the
outer function can only see g.

It says the code outside of the outer function can only see g. Does it mean that the code “var g = 100” cannot see anything else except “var g = 100” ?

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@rootNov 27.2017 — You will be amazed at how often this type of question pops up on this forum, a quick search will return you more examples and answers... ?

Suggested reading on the use of [B]var[/B] in functions and in the global scope too.
Copy linkTweet thisAlerts:
@wbportNov 27.2017 — Yes. Nothing can reference any variable defined in a function except code [B]in[/B] that function.
×

Success!

Help @mizocoder spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 10.12,
social: @webDeveloperHQ,
});

legal: ({
terms: of use,
privacy: policy
analytics: Fullres
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @aldoushuxley,
tipped: article
amount: 1000 SATS,

tipper: Anonymous,
tipped: article
amount: 1000 SATS,

tipper: @linalla,
tipped: article
amount: 1000 SATS,
)...