Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript concerns of using 'var' on an already-existing variable

Let's say I start up a var for an Object in my code

var base = {x:Infinity, y:Infinity};

and later in the same scope I do

var base = {x:0, y:0}

I get that I can re-use the existing variable, but I'm trying to iterate on a point.

  1. Does this cause any problems for the current scope as far as memory is concerned?
  2. Would there be any problems if these are in different scopes?
  3. Is the general rule to never use var on an already-existing variable?
  4. If I did this several (thousands) of times, would I run into any "funky" issues?
like image 972
Jacksonkr Avatar asked Dec 05 '11 21:12

Jacksonkr


People also ask

Why VAR should not be used in JavaScript?

Scoping — the main reason to avoid var var variables are scoped to the immediate function body (hence the function scope) while let variables are scoped to the immediate enclosing block denoted by { } .

Should I ever use VAR in JavaScript?

There will be times when you need var, like when you need a temp variable to be available within the scope of a block inside of a function, otherwise, preferring let over var will help developers with naming conflicts. On a lighter note, it's about time that ES6 introduced let .

Why do people still use VAR in JavaScript?

For you question directly, var is probably still used for legacy reasons. It is supported in all versions of JavaScript and it would be a bother to change every example on the internet. The only real advantage to var is it's compatibility.

What happens if you use a named variable without first declaring it using the VAR let or const keywords?

Variables can be declared and initialize without the var keyword. However, a value must be assigned to a variable declared without the var keyword. The variables declared without the var keyword becomes global variables, irrespective of where they are declared. Visit Variable Scope in JavaScript to learn about it.


2 Answers

  1. It releases its hold on the old value, so it can be potentially garbage collected. I say potentially, because objects can be referenced by different variables.

  2. If they're in different scopes, the inner base will shadow the outer one (assuming you're talking about nested scopes), so the old value will not be overwritten. Shadowing is usually best avoided.

  3. Generally, within the same scope, yes but that's more of a coding style aspect. It won't make an effective difference in the execution since there's only one declaration no matter how many times you declare the same var in the same scope.

  4. Depends. Are you talking about overwriting in a loop? If you no longer need the current value, it shouldn't be an issue.


To be clear, when you do this:

var base = {x:Infinity, y:Infinity};

var base = {x:0, y:0}

What's really happening is this:

var base;  // declaration is hoisted

base = {x:Infinity, y:Infinity};  // new object is referenced by base

base = {x:0, y:0}  // previous object is released, and new object is referenced

Note that when I talk about scope and var, I'm talking specifically about function scope. In standard JavaScript/ECMAScript implementations, there is no block scope, so there's no special meaning when using var in a for statement for example.

As mentioned by @Charles Bailey, Mozilla has let, which does allow scoping in blocks, but it would require that specific keyword. The var statement doesn't recognize the block with respect to variable scope.

like image 97
RightSaidFred Avatar answered Oct 18 '22 18:10

RightSaidFred


There is absolutely no problem in doing that. Afterall, variables declared within a function (-context) are just part of the such called Activation Object (ES edition 3). It is a little bit different in ES5, but for this part it is basically the same.

You are just overwritting a property, in an Object (not a Javascript object, but from the underlaying js engine).

So again, that is not a problem at all (even if its unusual und looks confusing).

If you declare a variable in another "scope" (which basically means, another function here), it is also no problem. Each (execution) context is completely on its own and cannot interfer with another context, unless we are talking about closures and stuff. But even then, if you have several functions and parent context which all have the same named variable, the lookup process (name resolution) always begins in your local scope (the Activation object from the current context) and after that, it goes into the "scope chain".

I guess that should answer 3.) and 4.) also. You "can" re-var a variable, but it makes no sense. So I'd just recommend to don't do it anymore.

like image 37
jAndy Avatar answered Oct 18 '22 17:10

jAndy