Local variables cannot be accessed outside the functions in which they are declared.

Scope of Variables

A scope is a region of the program and broadly speaking there are three places, where variables can be declared:

  • Inside a function or a block which is called local variables,
  • In the definition of function parameters which is called formal parameters.
  • Outside of all functions which is called global variables.
  • Here let us explain what local and global variables are.

    Local Variables

    Variables that are declared inside a function or block are local variables. They can be used only by statements that are inside that function or block of code. Local variables are not known to functions outside their own. Following is the example using local variables:

    #include <iostream>
    using namespace std;
     
    int main ()
    {
      // Local variable declaration:
      int a, b;
      int c;
     
      // actual initialization
      a = 10;
      b = 20;
      c = a + b;
     
      cout << c;
     
      return 0;
    }
    
    Global Variables

    Global variables are defined outside of all the functions, usually on top of the program. The global variables will hold their type throughout the life-time of your program.

    A global variable can be accessed by any function. That is, a global variable is available for use throughout your entire program after its declaration. Following is the example using global and local variables:

    #include <iostream>
    using namespace std;
     
    // Global variable declaration:
    int g;
     
    int main ()
    {
      // Local variable declaration:
      int a, b;
     
      // actual initialization
      a = 10;
      b = 20;
      g = a + b;
     
      cout << g;
     
      return 0;
    }
    
    

    A program can have same name for local and global variables but value of local variable inside a function will take preference. For example:

    #include <iostream>
    using namespace std;
     
    // Global variable declaration:
    int g = 20;
     
    int main ()
    {
      // Local variable declaration:
      int g = 10;
     
      cout << g;
     
      return 0;
    }
    
    

    When the above code is compiled and executed, it produces the following result:

    10

    What is Local Variable in Javascript

    Local variables are variables that can only be accessed within the function or block statement that they have been declared in. Block statements include if, if else, while, for and do while.

    Declare Local Variable in Javascript

    You can use var, let, and const keywords to create local variables within a function.

    function functionWithLocalVariable(){
      var username ="OscarKittyKat";
      const name="Oscar Wilden";
      let estimatedWorth2022 = 2585000;
    }
    
    functionWithLocalVariable();
    

    If you log the variables inside the function, you will get their values.

    Code

    function functionWithLocalVariable(){
      var username ="OscarKittyKat";
      const name="Oscar Wilden";
      let estimatedWorth2022 = 2585000;
      console.log(username);
      console.log(name);
      console.log(estimatedWorth2022);
    }
    
    functionWithLocalVariable();
    

    Result

    Local variables cannot be accessed outside the functions in which they are declared.

    These variables cannot be accessed outside the function. A quick check using a console log shows the variable as not defined outside of the function.

    function functionWithLocalVariable(){
      var username ="OscarKittyKat";
      const name="Oscar Wilden";
      let estimatedWorth2022 = 2585000;
    }
    
    functionWithLocalVariable();
    console.log(username);
    console.log(name);
    console.log(estimatedWorth2022);
    

    Result

    Local variables cannot be accessed outside the functions in which they are declared.

    To create local variables within a block, you can use only use let and const keywords. Block scope was introduced in the ES6 Javascript version. You can use the power of block scope with if, if else, while, for and do while statements.

    var age = 17;
    if(age<18){
      const designation = "Child";
    } else {
      const designation = "Adult";
    }
    

    The variables cannot be accessed outside the block. Checking the variable designation using a console log shows the variable as not defined outside the if else block but gives a result of child when read inside the block it is declared in.

    var age = 17;
    if(age<18){
      const designation = "Child";
      console.log(designation);
    } else {
      const designation = "Adult";
      console.log(designation);
    }
    console.log(designation);
    

    Result

    Local variables cannot be accessed outside the functions in which they are declared.

    Example of Local Variable in Javascript

    Local Variable Within Function and Block Example

    function getWordExistInPage(){
      let totalpages = 2;
      for(let page=0; page<totalpages; page++){
        console.log(page);
        console.log(totalpages);
    
      }
      console.log(totalpages);
      console.log(page);
    }
    getWordExistInPage();
    console.log(totalpages);
    

    page is a local block scope variable inside for loop while totalpages is a local function scope variable inside function getWordExistInPage.

    You can only read the value of page within the for loop while you can only read the value of totalpages inside the function getWordExistInPage.

    Result

    Local variables cannot be accessed outside the functions in which they are declared.

    How to Access Local Variable in Javascript

    To get access to local variables in Javascript, you can make a function call within the function that has the variables and pass the variable as a function parameter.

    Example

    function functionWithLocalVariable(){
      let localVariable = "I am a local Variable Being passed to another function";
      functionThatUsesTheLocalVariable(localVariable);
    }
    
    function functionThatUsesTheLocalVariable(variable){
      console.log(variable);
    }
    
    functionWithLocalVariable();
    

    Result

    Local variables cannot be accessed outside the functions in which they are declared.

    how to Access Local Variable Outside Function in Javascript

    You can pass local variable to another function by making the variable the return value of the function.

    When you call the function within another function this new function will get the value of the variable.

    Example

    function functionWithReturnValue(){
      let independenceYear = 1963;
      return independenceYear;
    }
    
    function getHowManyYearsSinceIndependence(){
      let yearsSinceIndependence = 2022 - functionWithReturnValue());
      console.log(yearsSinceIndependence);
    }
    
    getHowManyYearsSinceIndependence();
    

    Result

    Local variables cannot be accessed outside the functions in which they are declared.

    Difference Between Local and Global Variable in Javascript

    Global variables can be accessed by any part of you code on that file while local variables can only be accessed by code within the function or block they are declared in.

    You can read my on global variable and how to create variables in Javascript.

    Local variables cannot be accessed outside the functions in which they are declared.

    Hi there! I am Avic Ndugu.

    I have published 100+ blog posts on HTML, CSS, Javascript, React and other related topics. When I am not writing, I enjoy reading, hiking and listening to podcasts.

    Can local variables be accessed outside the functions in which they are declared?

    Local Variables A local variable is defined inside a function or a block statement and is not accessible outside the function or block. This means that these variables are visible and can only be used by the function in which the variable is defined!

    Can a local variable can be accessed from anywhere in the program?

    A local variable can be accessed from anywhere in the program. Different functions can have local variables with the same names. Python allows you to pass multiple arguments to a function. To assign a value to a global variable in a function, the global variable must be first declared in the function.

    How do you access local variables outside the method?

    Local Variables (Method Level Scope) Variables declared inside a method have method level scope and can't be accessed outside the method.

    Can you declare local variables?

    Declaring Local Variables You can declare them at the start of the program, within the main method, inside classes, and inside methods or functions. Depending on where they are defined, other parts of your code may or may not be able to access them. A local variable is one that is declared within a method.