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.

0   0
Shailesh Kumar
profile Lian 4th November 2024

Got my b3 bomber coat from Gleam Hide, and it’s been perfect for those freezing mornings!

Write a comment ...
Post comment
Cancel
profile 7th October 2024

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.

Write a comment ...
Post comment
Cancel
profile Mike 20th September 2024

I like your article very much, thanks for sharing the good information we have read.The Crow Jacket

Write a comment ...
Post comment
Cancel
profile 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

Write a comment ...
Post comment
Cancel

Please rotate your device

We don't support landscape mode on your device. Please rotate to portrait mode for the best view of our site