Javascript:Knowing the scope of your local and global variables
If a variable is declared outside a function, it’s GLOBAL. If it’s declared inside a function, it’s LOCAL.
GLOBAL variables
Example of global variables:
var avatar;
var levelThreshold = 1000;
These are global variables; they’re accessible everywhere in your JavaScript code.
LOCAL variables
Example of local variables:
function getScore(points) {
var score;
var i = 0;
while (i < levelThreshold) {
//code here
i = i + 1;
alert(i);
}
return score;
}
The "points", "score" and "i" variables are all declared within a function.
We call them local variables because they are known locally only within the function itself.
Even if we use levelThreshold inside the function, it’s global because it’s declared outside the function.
If a variable is declared outside a function, it’s GLOBAL. If it’s declared inside a function, it’s LOCAL.
Jaxon
14th October 2025
Superbly written article, if only all bloggers offered the same content as you, the internet would be a far better place..I know your expertise on this. I must say we should have an online discussion on this. Writing only comments will close the discussion straight away! And will restrict the benefits from this information.Superbly written article, Morphin kaufen online
Jaxon
4th October 2025
I’ve been searching for some decent stuff on the subject and haven't had any luck up until this point, You just got a new biggest fan!..This is really nice to read..informative post is very good to read..thanks a lot! blue salt trick for men
Lian
4th November 2024
Got my b3 bomber coat from Gleam Hide, and it’s been perfect for those freezing mornings!
This bomber jacket boasts a classic military-inspired design that never goes out of style. The rich brown color, combined with the shearling lining, gives it a timeless appeal. Its detailed stitching and robust structure ensure longevity and resilience.
Mike
20th September 2024
I like your article very much, thanks for sharing the good information we have read.The Crow Jacket
Jaxon
8th July 2024
Thank you because you have been willing to share information with us. Nice to read your article! I am looking forward to sharing your adventures and experiences.I have read all the comments and suggestions posted by the visitors for this article are very fine,We will wait for your next article so only.Thanks! https://www.digitekprinting.com/foam-core-posters
Post Your Message