Developer Programming Foundations: Learn variable scope


show more Variables are a key part of JavaScript, or any programming language, and understanding variable scope is essential in mastering the foundation of programming. Variables created within functions are only defined within that function and thus it is important to fully understand the function, the variable, and who can see it. In this video, Simon goes over the importance of variable scope and how to best define it. show less
please wait ...

Understanding variable scope

So one more thing you should be aware of when working with functions and this goes not just for JavaScript, but for most modern programming languages. If you create a function and define a variable inside that function using the word var, that variable is only available inside that function. This is because of something called variable scope. And every variable has a scope. That simply means where is this variable visible? Who can see it? Who can use it? So let's say we define this simple function and then we call it.

What's going to happen is we will jump into the function, we will create the variable, and we will pop up the alert box with the contents of that variable which is 500. Control returns to right after we called it and if try and pop up the content of x again, this will be regarded as undefined. It will say I don't know what x means, because the x variable is not available outside simple function. This is because variables declared inside the functions are referred ...

Understanding variable scope
Video duration: 2m 23s 4h 47m Beginner

Viewers:

Variables are a key part of JavaScript, or any programming language, and understanding variable scope is essential in mastering the foundation of programming. Variables created within functions are only defined within that function and thus it is important to fully understand the function, the variable, and who can see it. In this video, Simon goes over the importance of variable scope and how to best define it.

Subjects:
Developer Web
Author:
please wait ...