For example, in C#, I can do this:
String someString = null; var whatIWant = someString ?? "Cookies!";
var someString = null; var whatIWant = someString ? someString : 'Cookies!';
Which is sorta icky IMHO. Can I do better?
undefined, and otherwise returns its left-hand-side operand.
Please check compatibility before using it.
??) is using a logical OR (
var whatIWant = someString || "Cookies!";
Regardless of the type of the first operand, if casting it to a Boolean results in
false, the assignment will use the second operand. Beware of all the cases below:
alert(Boolean(null)); // false alert(Boolean(undefined)); // false alert(Boolean(0)); // false alert(Boolean("")); // false alert(Boolean("false")); // true -- gotcha! :)
var whatIWant = null || new ShinyObject(); // is a new shiny object var whatIWant = undefined || "well defined"; // is "well defined" var whatIWant = 0 || 42; // is 42 var whatIWant = "" || "a million bucks"; // is "a million bucks" var whatIWant = "false" || "no way"; // is "false"