I have 3 variables, that are not always set. o1, o2, o3.
I want to create a new variable as follows:
if none are set: o4 = null
if o1 is set, o4 = o1, regarded of what the other are.
when o1 is not set and o2 is set: o4 = o2
So o4 = o3 only gets set when 1 and 2 are not set.
==
I am now using a long switch, but I bet it can be done better.
JavaScript / jQuery
var tmpl = '';
if(data.o3 != null)
tmpl = data.o3;
if(data.o2 != null)
tmpl = data.o2;
if(data.o1 != null)
tmpl = data.o1;
Try
var o4 = o1 || o2 || o3.Edit: As per comments, this only works when values that might be set are not falsy. May be best to avoid this method, unless you’re sure.