Local and Global Variables in JavaScript -

Local and Global Variables in JavaScript

Local and Global Variables in JavaScript

In the realm of JavaScript programming, variables play a pivotal role in storing and manipulating data. They act as placeholders for values that can be referenced and modified throughout a program. However, not all variables are created equal. In JavaScript, variables are categorized into two main types: local variables and global variables. Understanding the distinction between these two types is essential for writing efficient and maintainable code.

Local Variables

Local variables are declared within a specific scope, typically within a function. These variables are accessible only within the block of code in which they are defined. Once the function execution is complete, local variables cease to exist, and their values are no longer accessible. This encapsulation helps prevent naming conflicts and promotes modularity in code.

function myFunction() {
var localVar = 10;
document.write(localVar); // Output: 10
}

myFunction();
document.write(localVar); // Error: localVar is not defined

In the example above, localVar is declared inside the myFunction() function, making it a local variable. It can be accessed and modified only within the function.

Global Variables

On the other hand, global variables are declared outside of any function and are accessible from anywhere within the script. They have a global scope, meaning they can be accessed by any function or block of code in the entire script. While global variables offer convenience in terms of accessibility, they also come with certain drawbacks. They can lead to naming conflicts, increase the risk of unintended side effects, and make it challenging to understand and maintain code.

var globalVar = 20;

function myFunction() {
document.write(globalVar); // Output: 20
}

myFunction();
document.write(globalVar); // Output: 20

In this example, globalVar is declared outside of any function, making it a global variable. It can be accessed from both inside and outside of the function myFunction().

local and global variables in javascript with example

Example 1-

<html>
<head>
<script>
var a=1000;//global variable
function MyFun()
{
var a=200;//local varible
document.write(“value of a in local scope=”+a+”<br>”);
}
</script>
</head>
<body>
<script>
document.write(“value of a in global scope=”+a+”<br>”);
MyFun();
</script>
</body>
</html>

Output

value of a in global scope=1000
value of a in local scope=200

Example-2

<html>
<head>
<script>
//how global variable used in local scope
var a=1000;//global variable
var b=2000;
function MyFun()
{
var a=200;//local varible
document.write(“value of a in function =”+a+”<br>”);//200
document.write(“value of b in function =”+b+”<br>”);//2000
}
</script>
</head>
<body>
<script>
document.write(“value of a before function calling=”+a+”<br>”);//1000
document.write(“value of b before function calling=”+b+”<br>”);//2000
MyFun();
document.write(“value of a after function calling=”+a+”<br>”);//1000
document.write(“value of b after function calling=”+b+”<br>”);//2000
</script>
</body>
</html>

Output

For Live Class Watch Full Video on youtube by click on the below link

https://youtu.be/M4CD03oh0gw

Difference between local and global variables in javascript

Scope:

  • Local Variables:
    • Local variables are declared within a specific block of code, typically within a function.
    • They are accessible only within the function or block in which they are defined.
    • Attempting to access a local variable from outside its scope will result in an error.
  • Global Variables:
    • Global variables are declared outside of any function or block.
    • They are accessible from anywhere within the script, including inside functions.
    • Global variables have a global scope, meaning they can be accessed by any part of the code.

Accessibility:

  • Local Variables:
    • Local variables are accessible only within the function or block in which they are declared.
    • They cannot be accessed from outside their containing function.
  • Global Variables:
    • Global variables are accessible from anywhere within the script.
    • They can be accessed and modified from any part of the code, including inside functions.

Lifetime:

  • Local Variables:
    • Local variables have a limited lifetime. They are created when the function in which they are declared is called, and they cease to exist once the function execution is complete.
    • Each time the function is called, a new instance of the local variable is created.
  • Global Variables:
    • Global variables persist throughout the entire lifetime of the program.
    • They are initialized when the script starts executing and remain in memory until the script terminates or the page is refreshed.

Best Practices

While both local and global variables have their use cases, it’s generally recommended to minimize the use of global variables whenever possible. Instead, opt for local variables, as they promote better encapsulation and reduce the likelihood of naming conflicts and unintended side effects. Additionally, using local variables can improve the performance of your code by limiting the scope of variables to only where they are needed.

When using global variables, exercise caution and ensure that their usage is justified. Avoid creating global variables unnecessarily, and consider alternative approaches such as passing values as function parameters or encapsulating related functionality within objects.

In conclusion, understanding the differences between local and global variables is crucial for writing clean, maintainable JavaScript code. By leveraging local variables within appropriate scopes and minimizing the use of global variables, you can write more robust and efficient code that is easier to understand and maintain.

Also Check Our Latest Uploads Related to JavaScript

javascript functions examples for practice

JavaScript Break and Continue (Examples)

 

Leave a Comment