Variable Scope in Javascript
Variable Scope in Javascript
In this article we are goanna to see about what is variable scope in Javascript and how many types is it and where we use these. which is most important when you are going to start your career in JavaScript. Please read and enjoy this article if you want to give any feedback about your experience, we welcome.
Scope in JavaScript refers to the current context of code, which determines the accessibility of variables to JavaScript.
The two types of scope are local and global:
Global variables are those declared outside of a block.
Global Scope
A variable that is declared outside a function that known as global variable and it's value can be access and modify throughout program.
Ex:
var x = 20,
function myfunction(y){
document.write(x + y);
}
Myfunction(20)
Local Scope
Variable that is declared inside a function that known as a local scope. It is created and destroy every time the function is executed, and it cannot be accessed by any code outside the function. Inside a function, if a variable has not been declared with var it is created as global variable.
Ex:
function myfunction(y){
var x = 20;
document.write(x + y);
}
myfunction(30);
Tags:
JavaScript
0 comments
Please leave your comments...... Thanks