Open In App

What is function scope in JavaScript?

Last Updated : 14 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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; // Variable x has function scope
  console.log(x); // Output: 10
 
  function innerFunction() {
    console.log(x); // Output: 10
  }
 
  innerFunction();
}
 
exampleFunction();
// console.log(x); // Error: x is not defined


Output

10
10


Similar Reads

Explain Scope and Scope Chain in JavaScript
In this article, we will try to understand what is the scope of a variable as well as its function (or method). We will see what is a Scope Chain with the help of certain coding examples. ScopeScope in JavaScript determines the accessibility of variables and functions at various parts of one's code or program.In other words, Scope will help us to d
5 min read
Explore the concept of JavaScript Function Scope and different types of JavaScript Functions
JavaScript is based on functional programming. Therefore, functions are fundamental building blocks of JavaScript. So the function is called first-class citizens in JavaScript. Syntax: Define a functionfunction functionName(parameters) { // SET OF STATEMENTS } Call a functionfunctionName(arguments); The function execution stops when all the stateme
10 min read
Scope : Variable Masking in JavaScript
In this article, we will learn about Variable Masking which is also known as Variable Shadowing i.e. a variable with the same name will shadow the variable in the outer scope. When a variable is masked, the masked variable is completely inaccessible using that variable name. The scope is hierarchical thus we can enter new scope without hampering th
3 min read
Lexical Scope in JavaScript
Lexical scope is a fundamental concept in programming that determines the accessibility of variables and functions within a program. In simple terms, the lexical scope is the scope of a variable or function based on where it is defined in the source code. The scope is determined by the placement of variables and functions in the code, and it remain
4 min read
Javascript Scope
JavaScript Scope is the area where a variable (or function) exists and is accessible. We can layer the scope in a system which means the child scope can access the parent scope but not vice-versa. Javascript has three different scopesGlobal ScopeBlock ScopeFunction ScopeGlobal ScopeThose variables which are declared outside the function or blocks o
3 min read
Public, Private, and Protected Scope in JavaScript
In this article, we will see the fundamentals of public, private, and protected scopes in JavaScript. We'll explore how to create these scopes, their practical applications, and the conventions used to simulate access control. Table of Content Private ScopePublic ScopeProtected Scope[caption width="800"]Scopes[/caption]Private Scope:Private scope i
4 min read
Difference Between Scope and Closures in JavaScript
The scope and closures are fundamental concepts that play a crucial role in how variables are accessed and managed within the functions and blocks of code. In this article, we will learn about the difference between scope and closures in JavaScript. What is Scope?The Scope in JavaScript refers to the context or environment in which a variable or fu
2 min read
What is local scope in JavaScript?
In JavaScript, local scope refers to the scope of variables or identifiers defined within a specific block of code, typically within a function or a block statement (denoted by curly braces {}). Variables declared within a local scope are only accessible within that scope and are not visible to code outside of it. Example: Here, the variable localV
1 min read
What is block scope in JavaScript?
Block scope in JavaScript refers to the scope of variables and functions that are defined within a block of code, such as within a pair of curly braces {}. Variables and functions declared with let and const keywords have block scope. Variables declared inside a block are only accessible within that block and any nested blocks. They are not accessi
1 min read
What is Variable Scope in JavaScript ?
Variable scope is the context of the program in which it can be accessed. In programming, a variable is a named storage location that holds data or a value. Think of it as a container that you can use to store and manipulate information in your code. Variables allow you to work with data in a flexible way, as the values they hold can change during
4 min read