Redeclaring a javascript variable

xralf picture xralf · Jul 31, 2011 · Viewed 19.1k times · Source

In this tutorial there is written:

If you redeclare a JavaScript variable, it will not lose its value.

Why should I redeclare a variable? Is it practical in some situations?

thank you

Answer

ThatGuy picture ThatGuy · Jul 31, 2011

It's nothing more than a reminder that if you do this:

var x=5;
var x;
alert(x);

Result will be 5.

If you re-declare variable in some other languages for example - result will be undefined, or NaN, but not in javascript.