-5

I am trying to determine what happens when I write two of the same exact variable names with different values.

var helloWorld = 'Hello';
var helloWorld = 'HelloWorld';

I know the current value is 'HelloWorld' but what happens to the first var helloWorld? Is that just re-assigned? Or is the first case Garbage Collected?

HelloWorld
  • 10,529
  • 10
  • 31
  • 50
  • 2
    See: http://stackoverflow.com/questions/4324133/javascript-garbage-collection – armadadrive May 27 '14 at 19:45
  • 2
    a little bit of research effort should have gotten you an answer – charlietfl May 27 '14 at 19:46
  • A [search with poor terms](https://duckduckgo.com/?q=Rewriting+a+variable%2C+what+happens+to+the+old+reference%3F) doesn't necessarily bring up the answer. – worc May 27 '14 at 19:50
  • Yeah, I tried searching 'declaring two variables' and a few other terms but couldn't find a definitive answer. Thanks for the help. – HelloWorld May 27 '14 at 19:53

1 Answers1

2

There is no "first var" here. The two var declarations are hoisted into one declaration and the second just becomes a simple assignment. The old value will eventually be garbage collected if it's not referred to anywhere else (which is pretty much the minimum you could expect from a garbage collector).

Chuck
  • 234,037
  • 30
  • 302
  • 389