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
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
Keep in mind that only variables declared with var can be re-declared. If you try to re-declare a variable declared with let or const (which is the ES2015 Javascript syntax that should be used in most cases nowadays), even worse than losing the value, an error will be thrown:
let foo = 'foo';
let foo;
So, in codebases which use modern Javascript syntax, re-declaring a variable simply isn't possible - the interpreter needs to be able to identify a single point in the code after which a let or const variable gets properly initialized. Before that point, the variable name will exist in the temporal dead zone.
It is pretty simple re-declaring doesn't actually affect anything, you just have to remember that if you reassign value within scope then the reassigned value is limited to scope and outside of scope it will still be globally declared value
var page =1 ;
function htmlcode(page) {
page = "keka";
console.log("inside " + page);
}
htmlcode(page);
console.log("inside " + page);
Output : inside keka
inside 1