Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • JavaScript - Get Started
  • What is JavaScript
  • Setup JavaScript Dev Environment
  • HTML <script> Tag
  • JavaScript - Syntax
  • JavaScript - Popup Message
  • JavaScript - Variables
  • JavaScript - Operators
  • JavaScript - Data Types
  • JavaScript - String
  • JavaScript - Numbers
  • JavaScript - Boolean
  • JavaScript - Object
  • JavaScript - Date
  • JavaScript - Date Methods
  • JavaScript - Array
  • JavaScript - Array Methods
  • JavaScript - null and undefined
  • JavaScript - Function
  • JavaScript - if condition
  • JavaScript - switch
  • JavaScript - for loop
  • JavaScript - while loop
  • JavaScript - Scope
  • JavaScript - eval
  • JavaScript - Error Handling
  • JavaScript - strict mode
  • JavaScript - Hoisting
  • Define JS Class
  • JS Object In Depth
  • this Keyword
  • new Keyword
  • Prototype
  • Inheritance
  • Closure
  • IIFE
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

Scope in JavaScript

Scope in JavaScript defines accessibility of variables, objects and functions.

There are two types of scope in JavaScript.

  1. Global scope
  2. Local scope

Global Scope

Variables declared outside of any function become global variables. Global variables can be accessed and modified from any function.

Example: Global Variable
<script>

    var userName = "Bill";

    function modifyUserName() {
            userName = "Steve";
        };

    function showUserName() {
            alert(userName);
        };

    alert(userName); // display Bill
    
    modifyUserName();
    showUserName();// display Steve

</script>
Try it

In the above example, the variable userName becomes a global variable because it is declared outside of any function. A modifyUserName() function modifies userName as userName is a global variable and can be accessed inside any function. The same way, showUserName() function displays current value of userName variable. Changing value of global variable in any function will reflect throughout the program.

Please note that variables declared inside a function without var keyword also become global variables.

Example: Global Variable
<script>

    function createUserName() {
        userName = "Bill";
    }

    function modifyUserName() {
        if(userName)
            userName = "Steve";
    };

    function showUserName() {
        alert(userName);  
    }
    
    createUserName();
    showUserName(); // Bill 

    modifyUserName();
    showUserName(); // Steve 

    
</script>
Try it

In the above example, variable userName is declared without var keyword inside createUserName(), so it becomes global variable automatically after calling createUserName() for the first time.

A userName variable will become global variable only after createUserName() is called at least once. Calling showUserName() before createUserName() will throw an exception "userName is not defined".

Local Scope

Variables declared inside any function with var keyword are called local variables. Local variables cannot be accessed or modified outside the function declaration.

Example: Local Scope
<script>
    
    function createUserName() {
        var userName = "Bill";
    }

    function showUserName() {
        alert(userName);
    }

    createUserName();
    showUserName(); // throws error: userName is not defined

</script>
Try it

Function parameters are considered as local variables.

In the above example, userName is local to createUserName() function. It cannot be accessed in showUserName() function or any other functions. It will throw an error if you try to access a variable which is not in the local or global scope. Use try catch block for exception handling.

Some tips..

If local variable and global variable have same name then changing value of one variable does not affect on the value of another variable.

Example: Scope
var userName = "Bill";

function ShowUserName()
{
    var userName = "Steve";

    alert(userName); // "Steve"
}

ShowUserName();

alert(userName); // Bill
Try it

JavaScript does not allow block level scope inside . For example, variables defined in if block can be accessed outside if block, inside a function.

Example: No Block Level Scope
Function NoBlockLevelScope(){
    
    if (1 &gt; 0)
    {
        var myVar = 22;

    }

    alert(myVar);
}

NoBlockLevelScope();
Try it
Points to Remember :
  1. JavaScript has global scope and local scope.
  2. Variables declared and initialized outside any function become global variables.
  3. Variables declared and initialized inside function becomes local variables to that function.
  4. Variables declared without var keyword inside any function becomes global variables automatically.
  5. Global variables can be accessed and modified anywhere in the program.
  6. Local variables cannot be accessed outside the function declaration.
  7. Global variable and local variable can have same name without affecting each other.
  8. JavaScript does not allow block level scope inside brackets.
TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.