Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The lifetime of JavaScript global variables

I read the following on w3schools and I don´t understand what it means by the second part:

[global variables] remain available to new pages loaded into the same window.

Anyone can enlighten me ?

In a web browser, global variables are deleted when you close the browser window (or tab), but remain available to new pages loaded into the same window.

link: https://www.w3schools.com/js/js_scope.asp

like image 597
codeDragon Avatar asked Jan 22 '18 11:01

codeDragon


People also ask

What is the lifetime of a global variable?

In compiled languages, global variables are generally static variables, whose extent (lifetime) is the entire runtime of the program, though in interpreted languages (including command-line interpreters), global variables are generally dynamically allocated when declared, since they are not known ahead of time.

How would you describe the scope and lifespan of a JavaScript global variable?

A global variable has a global scope which means it can be defined anywhere in your JavaScript code. Global variables delete when the web browser is closed. However if a new page is loaded in the same browser window, then it remains.

Are global variables permanent?

Declaring a variable outside of any function body creates a global variable. Global variables remain permanently in scope and can be used by any code in the program because they're always bound to one specific memory location.

What is global variable in JavaScript?

A global variable is a variable that is declared in the global scope in other words, a variable that is visible from all other scopes. In JavaScript it is a property of the global object.


1 Answers

If w3schools really says that (heaven help us, it does), it's just plain wrong, as can easily be proved with a simple experiment.

When a web page loads, the global environment for the JavaScript on that page is created fresh. No globals from a previous page in that same window are available to the new page. Obviously, some other things provided by the browser (such as things stored in web storage) may be, but not global variables.

While it's improved a bit over time, unfortunately this isn't the first simple factual error people have noted on w3schools. I'd suggest using MDN's documentation instead. While MDN is community-edited and thus does sometimes have the occasional inaccuracy, over all it's quite good, and any inaccuracies one user adds tend to be fixed fairly quickly by others.

In a comment you've asked:

What's the official JavaScript documentation page ?

The only "official" documentation for JavaScript is the specification. Likewise, browser features like the DOM are also documented in various specifications. The WHAT-WG "HTML5 standard" (which is about a lot more than HTML) also documents HTML (obviously) and a wide range of other browser features. Others are in their own specs. One of the great things about MDN is that it links to specs.

like image 122
T.J. Crowder Avatar answered Sep 23 '22 22:09

T.J. Crowder