Skip to content

Instantly share code, notes, and snippets.

@jashkenas
Created November 23, 2011 21:23
Show Gist options
  • Select an option

  • Save jashkenas/1389969 to your computer and use it in GitHub Desktop.

Select an option

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

ken210 commented Nov 23, 2011

Copy link
Copy Markdown

option #2

@dmnd

dmnd commented Nov 23, 2011

Copy link
Copy Markdown

2

@markdalgleish

Copy link
Copy Markdown

Option 2

@pkananen

Copy link
Copy Markdown

2

@mxriverlynn

Copy link
Copy Markdown

#2

@satyr

satyr commented Nov 23, 2011

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

@jblanche

Copy link
Copy Markdown

#2

@fat

fat commented Nov 23, 2011

Copy link
Copy Markdown

option 4

@DmitrySoshnikov

Copy link
Copy Markdown

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
Copy Markdown

#2

@strathmeyer

Copy link
Copy Markdown

#2

@satyr

satyr commented Nov 23, 2011

Copy link
Copy Markdown

@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
Copy Markdown

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

akavlie commented Nov 23, 2011

Copy link
Copy Markdown

@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
Copy Markdown

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

ghost commented Nov 23, 2011

Copy link
Copy Markdown

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

@seidtgeist

Copy link
Copy Markdown

#2

@valueof

valueof commented Nov 23, 2011

Copy link
Copy Markdown

#2

@RemyRylan

Copy link
Copy Markdown

#2

@andrewvc

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

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

@JasonOffutt

Copy link
Copy Markdown

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

@johnjbarton

Copy link
Copy Markdown

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
Copy Markdown

#2

@sumitngupta

Copy link
Copy Markdown

el numero dos.

@fat

fat commented Nov 24, 2011

Copy link
Copy Markdown

@andrewvc

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

@anthonywu

Copy link
Copy Markdown

#2

@ricardobeat

Copy link
Copy Markdown

@fat that's so pretty

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

@andrewvc

Copy link
Copy Markdown

@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

jxson commented Nov 24, 2011

Copy link
Copy Markdown

#2

@fat

fat commented Nov 24, 2011

Copy link
Copy Markdown

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