Web Development

JavaScript Scope

JavaScript Scope is section 3 of Beginner’s Guide to JavaScript.

Scope defines the areas where a variable can take their operations on. As programmer, you really need to be aware of your variables and where they can go. I mean their boundary of accessing things.

According to their access level, we can categorize variables as follows.

  • Local Variables

Variables declared within a JavaScript function, become LOCAL to the function. So we called them local variables. Local variables have local scope: They can only be accessed within the function.

[code language=”js”] // code here can not refer the same dog inside the function

function myFunction() {
var dog = “Timmy”;

// code here can use dog

}
[/code]

Since local variables are only recognized inside their functions, variables with the same name can be used in different functions or outside the function. Local variables are created in the memory when a function starts, and flushes away when the function is completed.

Function arguments what you know as parameters work as local variables inside functions and have the same behavior.

[code language=”js”]

function myFunction(dogName) { // dogName is a function argument
var dog = dogName;
}
[/code]

  • Global Variables

A variable declared outside a function, becomes GLOBAL. A global variable has global scope: All scripts and functions on the web page can access it. As well as can create a new variable with the same name inside of a function and the original will not be effected from it.

[code language=”js”] var dog = “Timmy”;
// code here can use dog
function myFunction() {

// code here can refer the same dog as well as create a new dog

}
[/code]

With JavaScript, the global scope is the complete JavaScript environment. In HTML, the global scope is the window object. All global variables belong to the window object.

window.dog

Automatically Global

If you assign a value to a variable that has not been declared, it will automatically become a GLOBAL variable.

[code language=”js”]

// code here can use dog
function myFunction() {
dog = “Timmy”;
// code here can refer the same dog but cannot create a new dog

}
[/code]

 

Lifetime of Variables

The lifetime of a JavaScript variable both of the above two we mentioned starts when it is declared. Local variables are flushed from the memory when the function is completed. Global variables are flushed from the memory when you close the page.


 

WHAT’S NEXT?

Let’s Learn JavaScript Events and Functions.

Join Our Newsletter Today

Stay updated with all latest updates.
Give it a try, you can unsubscribe anytime.
Subscribe
SUBSCRIBE NOW

About the author

Sandeep Likhar

Sandeep Likhar from Indore India, Blogger, eBook Designer and Founder of LetsDnD. Having five years of experience in industry as a Digital Publishing Expert and eBook Converter providing services to Authors, Publishers and distributors worldwide. Proficient in converting books into various formats HTML, epub, mobi, word, PDF including all major online platforms iTunes, Kobo, Kindle, Createspace, B&N, Smashwords and more.

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz
DON’T MISS OUT!
Subscribe To Newsletter
Be the first to get latest updates and exclusive content straight to your email inbox.
Stay Updated
Give it a try, you can unsubscribe anytime.

Join Our Newsletter Today

Stay updated with all latest updates.
Give it a try, you can unsubscribe anytime.
Subscribe
SUBSCRIBE NOW