In other languages I have two sets of operators, or
and ||
, which typecast differently. Does Javascript have a set of operators to compare and return the original object, rather than a boolean value?
I want to be able to return whichever value is defined, with a single statement like var foo = bar.name or bar.title
There is only one set of boolean operators (||
, &&
) and they already do that.
var bar = {
name: "",
title: "foo"
};
var foo = bar.name || bar.title;
alert(foo); // alerts 'title'
Of course you have to keep in mind which values evaluate to false.
var foo = (bar.name != undefined) ? bar.name :
((bar.title != undefined) ? bar.title : 'error');
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