I've always used the following syntax to ensure that the input variable isn't null.
function f(input){
if(input === null)
input = "";
...
}
Lately, I noticed that it's shorter to express it as follows.
function f(input){
input = input ? input : "";
...
}
But also, I've seen this syntax.
function f(input){
input = input || "";
...
}
- Are those equivalent (not in what they do but in how they do it)?
- Which is most recommended (readability etc.)?
Note that I'll be strictly working with inputs of strings such that it's either valid one or null (not provided at all). If I'd like to extend the protection to include other types, what additional issues should I take into consideration?
input || (input = "");