This doesn’t make sense to me, but I have a feeling I saw a code using this:
var abc = def || ghi;
My question is, is this valid? Can we add a condition to a variable declaration? I imagine the answer is no but I have this at the back of my mind that I saw something similar in code once.
This gives to
abcthe value ofdefif it isn’t falsy (i.e. notfalse,null,undefined,0or an empty string), or the value ofghiif not.This is equivalent to:
This is commonly used for options:
If you call
myfunc({mything:"bbb"})it uses the value you give. It uses"aaa"if you provide nothing.In this case, in order to let the caller wholly skip the parameter, we could also have started the function with