Skip to content

Instantly share code, notes, and snippets.

@jashkenas
Created November 23, 2011 21:23
Show Gist options
  • Save jashkenas/1389969 to your computer and use it in GitHub Desktop.
Save jashkenas/1389969 to your computer and use it in GitHub Desktop.
// How do you tend to declare variables in your code, when nested in an if / else?
// All three are functionally equivalent.
// (Ternary is cheating for these purposes.)
// Option 1:
function() {
if (condition) {
var attrs = a;
} else {
var attrs = b;
}
}
// Option 2:
function() {
var attrs;
if (condition) {
attrs = a;
} else {
attrs = b;
}
}
// Option 3:
function() {
if (condition) {
var attrs = a;
} else {
attrs = b;
}
}
@ken210
Copy link

ken210 commented Nov 23, 2011

option #2

@dmnd
Copy link

dmnd commented Nov 23, 2011

2

@markdalgleish
Copy link

Option 2

@pkananen
Copy link

2

@mxriverlynn
Copy link

#2

@satyr
Copy link

satyr commented Nov 23, 2011

function option4(){
  var attrs = condition ? a : b
}

@jblanche
Copy link

#2

@fat
Copy link

fat commented Nov 23, 2011

option 4

@DmitrySoshnikov
Copy link

We definitely need expression forms in JS for such cases. And not only with simple ifs which can be managed by ?: op. But for all of them: if, switch, try, blocks, etc. The discussion was/is here: https://mail.mozilla.org/pipermail/es-discuss/2011-November/018222.html

And for now, regarding JS, variant #2 is OK.

Dmitry.

@dariocravero
Copy link

#2

@strathmeyer
Copy link

#2

@satyr
Copy link

satyr commented Nov 23, 2011

@DmitrySoshinkov: Agreed. The point of option 4 above is that simple if doesn't make good example for the topic. I personally use option 3 for similar cases.

try { var result = run() }
catch(e){ result = e }

@eliperelman
Copy link

If the only variables in the current scope are the ones created for a loop, then I will create them in the loop statement, otherwise it's single var at the top:

var fn = function (arr) {
    for (var i = 0, l = arr.length; i < l; i++) { 
        // operations
    }
};

var fn = function (arr) {
    var args = [].slice.call(arguments),
        i = 0,
        l = arr.length;

    for (; i < l; i++) { 
        // operations
    }
};

@akavlie
Copy link

akavlie commented Nov 23, 2011

@mythz Ah, didn't realize that vars declared within a conditional branch that's never executed were still hoisted like that. Thanks.

Good reference: http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoisting

@dariocravero
Copy link

@akavile, that reference is brilliant!! thx for sharing it!..

Copy link

ghost commented Nov 23, 2011

Option 2 all the way... will pass a jslint too.

@seidtgeist
Copy link

#2

@valueof
Copy link

valueof commented Nov 23, 2011

#2

@jrylan
Copy link

jrylan commented Nov 23, 2011

#2

@andrewvc
Copy link

var res = (function () {
  if (condition) {
    return a;
  } else {
    return b;
  })();

(Just kidding, I'm all about #2)

@JasonOffutt
Copy link

Crockford will destroy you with his mind if you choose 1 or 3.

@johnjbarton
Copy link

Every JS dev knows in the heart #1 should be the right answer. #2 is C and #3 is asking for trouble.

@narfdotpl
Copy link

#2

@sumitngupta
Copy link

el numero dos.

@fat
Copy link

fat commented Nov 24, 2011

@andrewvc

var res = (function () {
  if (condition) return a
  return b
}())

@anthonywu
Copy link

#2

@ricardobeat
Copy link

@fat that's so pretty

 var a = (function(){ return 'value'; }).call(this);

@andrewvc
Copy link

@fat I never, ever, leave braces off when optional in JS. I'll take the line-noise for security. if w/o braces only works when it's at the end of a line a la Ruby.

@jxson
Copy link

jxson commented Nov 24, 2011

#2

@fat
Copy link

fat commented Nov 24, 2011

I never, ever, leave braces off when optional in JS

@andrewvc

http://i.imgur.com/NaI4O.gif

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment