Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there downsides to using var more than once on the same variable in JavaScript

Tags:

javascript

Before asking my question, let me give a disclaimer. I know what var does, I know about block scope, and I know about variable hoisting. I'm not looking for answers on those topics.

I'm simply wondering if there is a functional, memory, or performance cost to using a variable declaration on the same variable more than once within a function.

Here is an example:

function foo() {
  var i = 0;
  while (i++ < 10) {
    var j = i * i;
  }
}

The previous could just have easily been written with the j variabled declared at the top:

function foo() {
  var i = 0, j;
  while (i++ < 10) {
    j = i * i;
  }
}

I'm wondering if there is any actual difference between these two methods. In other words, does the var keyword do anything other than establish scope?

Reasons I've heard to prefer the second method:

  1. The first method gives the appearance of block scope when it's actually function scoped.
  2. Variable declarations are hoisted to the top of the scope, so that's where they should be defined.

I consider these reasons to be good but primarily stylistic. Are there other reasons that have more to do with functionality, memory allocation, performance, etc.?

like image 336
Philip Walton Avatar asked Mar 26 '13 21:03

Philip Walton


People also ask

What was the drawback of VAR in JavaScript?

Scoping — the main reason to avoid var console. log(baz); // ReferenceError}run(); The reason why the let keyword was introduced to the language was function scope is confusing and was one of the main sources of bugs in JavaScript.

Why you should not use VAR in JavaScript?

The Var Keyword This means that if a variable is defined in a loop or in an if statement it can be accessed outside the block and accidentally redefined leading to a buggy program. As a general rule, you should avoid using the var keyword.

Can we declare same variable in two times?

We cannot declare the same variable multiple times if one of them is declared using let, whereas we can declare the same variable any number of times using var.

Can I declare a variable twice in JavaScript?

You should not re-declare same variable twice. JavaScript is untyped language. This means that a JavaScript variable can hold a value of any data type.


1 Answers

In JavaScript - The Good Parts Douglas Crockford suggests that by using the second method and declaring your variables at the top of their scope you will more easily avoid scope bugs.

These are often caused by for loops, and can be extremely difficult to track down, as no errors will be raised. For example;

function() {
  for ( var i = 0; i < 10; i++ ) {
    // do something 10 times
    for ( var i = 0; i < 5; i++ ) {
      // do something 5 times
    }
  }
}

When the variables are hoisted we end up with only one i. And thus the second loop overwrites the value, giving us an endless loop.

You can also get some bizarre results when dealing with function hoisting. Take this example:

(function() {
  var condition = true;
  if(condition) {
    function f() { console.log('A'); };
  } else {
    function f() { console.log('B'); };
  }
  f(); // will print 'B'
})();

This is because function bodies are hoisted and the second function overwrites the first.

Because searching for bugs like this is hard and regardless of any performance issues (I rarely care about a couple of microseconds), I always declare my variables at the top of the scope.

like image 84
Jivings Avatar answered Oct 13 '22 00:10

Jivings