I am not familiar with the use of JS and I have the following doubt:
What is the difference between the following two examples (in terms of memory usage)?
Example 1:
var a = 3;
a = 7;
Example 2:
var a = 6;
var a = 9;
In both cases only one variable would have been declared? I'm asking this because I'm considering reusing variables to minimize memory usage.
Cheers!
In the first case you are declaring and assigning a value to your variable 'a' on the first line and reassigning another value on the second line,
while in the second you are declaring two variables with the same name, which should give you an error.
Whenever you can, declare your variables at the beginning of your script or the code block where it will be used and do not create more variables than necessary, at the memory level it will be more optimal, and for anyone who reads the code it will be much more understandable. .
The first case is redundant, since in the first line(
var a = 3;
) you already declare the value ofa
and in the second you assign the same value again.The second case should fail because you are creating two variables with the same name, regardless of their value.