Why Global variables Shouldn’t Be Much Global
Tags: showdev, devops, webdev
Published: 2022-07-13 09:57:00



One of the biggest blunder a JS developer can do while writing a code is declaring unnecessary global variables. Global variables are extremely helpful for the programmers but if not used carefully would rob the speed and efficiency of any browser.

## Short note

There are mainly two types of variables that are used in JS, Local and Global. Local variables are defined and used within a function whereas Global variables are defined for the function window. In short till the time the code doesn’t terminate Global variable will be present lurking in the background.

***Hey! Do you know? [CRC32 hash generator](https://www.lambdatest.com/free-online-tools/crc32-hash-calculator?utm_source=devto&utm_medium=organic&utm_campaign=jul13_sd&utm_term=sd&utm_content=free_tools) lets you quickly generate the CRC32 checksum hash from a given string. In addition, you can generate CRC32 hashes via your web browser.***

## Effect on Memory

Variables storing feeble data won’t be too critical ,however if you store a lot of data on it it’ll choke your bandwidth and definitely threaten the page efficiency. Too much of data stored as cache slows the speed of your browser resulting in high latency, caches are website’s data that is stored and used when you revisit the site time and again.

***Check this out: [The character count](https://www.lambdatest.com/free-online-tools/character-count?utm_source=devto&utm_medium=organic&utm_campaign=jul13_sd&utm_term=sd&utm_content=free_tools)- It allows programmers to calculate the length of any given string of text or numbers in order to check the total number of characters (including spaces) there are in the string.***

## Solution

We can’t deny the usefulness of global variables, however its best to try using local variables and use globals ones only in situations where it can’t be helped. However it’s important to change the value of the variables to NULL after using.

    function Avengers() {
        var hero = "Nick Fury";
                              }

Accidental global variables are created when you assign a value to a variable that hasn’t been declared, by default it will be a Global variable.

    function Avengers() {
               hero = "Nick Fury";
                              }

“Use strict”; is a brilliant way to keep the Global variables in check, just writing this magical command will solve half of the problems. Mostly used by developers, it doesn’t allow the menacing accidental variables by giving error if the variable hasn’t been declared.

    <h2>Global "use strict" declaration.</h2>
    <script>
    "use strict";
    Avengers();

    function Avengers() {
        hero = "Nick Fury";                    // This will cause an error (hero is not defined)
    }
    </script>

![](https://cdn-images-1.medium.com/max/2000/1*CNbgtyZWEqokJmYJPi8z2A.png)

***Do you know, in computing and electronic systems, a [binary-coded decimal (BCD)](https://www.lambdatest.com/free-online-tools/bcd-to-decimal?utm_source=devto&utm_medium=organic&utm_campaign=jul13_sd&utm_term=sd&utm_content=free_tools) is a method of representing each decimal digit by its binary sequence. The BCD to Decimal Converter (BDC) converts a binary-coded decimal (BCD) to an integer.***


## Conclusion

The use of global variables isn’t that global anymore. In the next blog we will further explore other memory leakage problems.