5 ms·
In my opinion this is a terrible idea, since it's very easy (I do it all the time) to accidentally write `if (foo = bar)` instead of `if (foo == bar)`. If that
by pault 7y ago
In my opinion this is a terrible idea, since it's very easy (I do it all the time) to accidentally write `if (foo = bar)` instead of `if (foo == bar)`. If that were valid syntax it would be a huge footgun. I'd be onboard with it if it required a different syntax.
- TazeTSchnitzel 7y agoBut JS already allows that.
- ubertaco 7y agoI'm down if you require a let/var/const in front of it: if (foo = bar()) { // syntax error! } if (let foo = bar()) { // works fine } if (const foo = bar()) { // also works fine } if (var foo = bar()) { // also also works fine }
- fenomas 7y ago> if (foo = bar()) { // syntax error! That's already an error in strict mode (which would presumably be on for anyone writing bleeding-edge JS).
- AgentME 7y agoIt's only an error in strict mode if there's no variable in scope named foo.
- dotancohen 7y agoAnd what if foo had already been defined in the scope?
- munificent 7y agoThe new one shadows it.
- dotancohen 7y agoLocal scope might not be what the programmer wants, though, depending on circumstance. Pseudocode: i = 3; foo = \0; while ( i-- ) { if ( foo = resultFromFooApi() ) { break; } sleep 1; } return foo;