Website Developer Question:
Download Job Interview Questions and Answers PDF
Do you know what Is Scope In JavaScript?
Answer:
The general meaning of scope is the accessibility of functions and variables in an application. Usually, we use them in two ways i.e. Local and Global.
A) Local Scope.
If we declare a function or variable inside a function, then we can access it only inside that function.
// code here can not use myLocalVar
function myFunction() {
var myLocalVar = "I'm Local";
// code here can use myLocalVar
}
B) Global Scope.
Declaring a variable anywhere on the page would mean that we can access it from any of the functions on that page.
var myGlobalVar = "I'm Global";
// code here can use myGlobalVar
function myFunction() {
// code here can use myGlobalVar
}
A) Local Scope.
If we declare a function or variable inside a function, then we can access it only inside that function.
// code here can not use myLocalVar
function myFunction() {
var myLocalVar = "I'm Local";
// code here can use myLocalVar
}
B) Global Scope.
Declaring a variable anywhere on the page would mean that we can access it from any of the functions on that page.
var myGlobalVar = "I'm Global";
// code here can use myGlobalVar
function myFunction() {
// code here can use myGlobalVar
}
Download Website Developer Interview Questions And Answers
PDF
Previous Question | Next Question |
Tell me how Do You Change The Style/Class On Any Element From JavaScript? | Tell me what Are == And === Operators In JavaScript And How Do They Differ? |