Image

Imageekatemba wrote in Imagejavascript

Listens: Mahler, Das Lied von der Erde

value after redeclaring var

If I redeclare a possibly existing variable using var but no assignment, is it guaranteed to keep its old value if it had one? Example:
var mylang = 'fr';
// lots of HTML and script in between
var mylang;
if (mylang == 'fr')
  document.write('Bienvenu...');
else
  document.write('Welcome...');

The reason I need to know is that I have headers and footers on every page, included with <script src=header.js>. If these are long and changeable, I don't want to make a separate copy headerfr.js that also needs updating every time: I want to parametrize just one header.js.

If I'm newly adding French pages, I can add var mylang = 'fr'; to them as I go, but I don't want to go back through all the English pages adding a var mylang = 'en'; just to allow the header.js to work. Also, there'll be other parameters in future, like images vs textonly. I want all my old pages to work without having these new vars in them. So I want to redeclare the var mylang inside header.js to ensure it works. But obviously I don't want this to reset its value to undefined: I want it to remain 'fr' if it already was. This works on my testing, but is it guaranteed to work?

Thanks in advance.