0

Possible Duplicate:
Javascript : assign variable in if condition statement, good practice or not?

Is it bad practice to assign/evaluate things inside an if statement?

eg.

var foo;
var bar = function() { .. }

if(foo = bar()) {
    ..
}
Community
  • 1
  • 1
kidcapital
  • 4,944
  • 8
  • 44
  • 67
  • You could have it all (excluding the `var` declaration) in the `if()` if you really want to: `var foo, bar; if( foo = (bar = function() { /***/ })() ) { /***/ }` – user113716 Jun 18 '11 at 18:26

2 Answers2

3

Functionality-wise there's absolutely nothing wrong with that. But if you're going for readability, keep in mind that more junior members of your team (if any) may have a tougher time with less straight forward syntax as this.

Kon
  • 26,469
  • 11
  • 58
  • 84
1

This will work fine and is not bad practice in Javascript.

JSLint might not agree with me though ;-)

Gaz
  • 1,510
  • 2
  • 17
  • 21