What is function scope in JavaScript?
Last Updated :
14 Feb, 2024
Function scope in JavaScript refers to the scope of variables and functions that are defined within a function. Variables and functions declared with the var
keyword have function scope.
Variables declared inside a function are accessible only within that function and any nested functions. They are not accessible outside of the function in which they are defined. This means that variables declared within a function cannot be accessed before their declaration or outside of the function.
Example: Here, the variable x
is declared inside the exampleFunction()
function. It is accessible within that function and also within the innerFunction()
nested inside it. However, x
is not accessible outside of exampleFunction()
.
Javascript
function exampleFunction() {
var x = 10;
console.log(x);
function innerFunction() {
console.log(x);
}
innerFunction();
}
exampleFunction();
|
Share your thoughts in the comments
Please Login to comment...