4

In Javascript, which format of var declarations is better:

function test1() {
  switch(type) {
    case 1:
        var test = "Hello One"
      break;
    case 2:
        var test = "Hello Two"
      break;
  }
}

Or:

function test2() {
  var test;

  switch(type) {
    case 1:
        test = "Hello One"
      break;
    case 2:
        test = "Hello Two"
      break;
  }
}

In test2(), there is 1 extra line of code to declare test as a var before assigning a value, but this saves having to declare var test twice. Is either way better than the other?

3
  • 1
    possible duplicate of javascript var declaration within loop because the answer explains what's happening here. (I'm sure there are dozens of other questions about hoisting.) Commented Aug 5, 2013 at 17:20
  • Well, actually I was asking more about the syntax of it, more than what was going on with the scoping. It's nice to learn about the term "javascript hoisting" though! Commented Aug 5, 2013 at 17:32
  • I get that, but the explanation of what's happening explains why one syntax is preferred. You sort of can't ask one without the other. Commented Aug 5, 2013 at 17:38

3 Answers 3

6

javascript does not have block scope, so declaring variables in a switch block does not work like you would expect.

furthermore, due to variable hoisting, all variable declarations in a function block are hoisted to the top by the interpreter and your code would look like this:

function test1() {
  var test;
  var test;

  switch(type) {
    case 1:
        test = "Hello One"
      break;
    case 2:
        test = "Hello Two"
      break;
  }
}

After performing the hoist, it is easy to see why the first block is incorrect.

Sign up to request clarification or add additional context in comments.

6 Comments

Could you clarify what you mean by "incorrect" in your final sentence? Both examples are valid and behave identically. (I'm not disagreeing with anything you're saying in this answer, but I think it would be helpful to use a more precise term than "incorrect" -- perhaps "less clear"?)
@apsillers "incorrect" meaning a bad practice that static analysis tools (jslint, jshint, resharper, etc) will flag
@apsillers is it really valid? (Is there a spec line stating that declaring the same variable more than once is acceptable, or is it just ignored in implementation?)
@Mathletics Yes, see EMCAScript 10.5, step 8: for each VariableDeclaration with some identier dn, the interpreter checks if a variable with identifier dn is already declared in-scope, and only acts if it does not yet exist.
@apsillers perfect, that's what I wanted to see. Thanks!
|
4

IMO the second should be preferred.

It's more communicative, and closer to what's actually happening (e.g., variable hoisting).

I'm also not a fan of hiding variable declarations inside what look like scopes but aren't.

Comments

2

Your first code is wrong; you've declared the same variable multiple times.

JSHint will complain about it.

1 Comment

The parser won't, though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.