Are these two statements equivalent?
var var1 = var2 ? var2 : 0;
var var1 = var2 || 0;
Seems like yes, however I'm not sure.
var2 is (probably) defined above.
No, they're not:
> var i=0;
> with({ get var2() { return ++i; } }) {
> var var1 = var2 || 0;
> }
> var1
1
> var i=0;
> with({ get var2() { return ++i; } }) {
> var var1 = var2 ? var2 : 0;
> }
> var1
2
As you can see, the second one evaluates var2
twice. However, this is the only difference, and one that hardly matters for "normal" variables.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With