I've found this string in JavaScript code.
var c = (a.b !== null) ? a.b : null;
This is a shorthand of an if-else statement, however the value null is assigned if it is null. Isn't that ALWAYS equivalent to
var c = a.b
including all cases - exceptions, null, undefined, etc?
In another words, are these lines (always) equivalent?
var c = (a.b !== null) ? a.b : null;
-vs-
var c = a.b
Now Undefined X 1 in JavaScript yields NaN(Not a Number) as result.
No, they AREN'T NECESSARILY EQUAL always if b is a getter that updates a variable. It's bad practice to code this way though
var log = 0; var a = { get b() { log++; return log; } } var c = (a.b !== null) ? a.b : null; // outputs 2 console.log(c);
var log = 0; var a = { get b() { log++; return log; } } var c = a.b; // outputs 1 console.log(c);
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