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
  • 5,064
  • 9
  • 46
  • 68
  • 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
  • 27,113
  • 11
  • 60
  • 86
1

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

JSLint might not agree with me though ;-)

Gaz
  • 1,570
  • 2
  • 17
  • 22