Turn string to FALSE if it is equal to ''

  • A+
Category:Languages

The code I have returns a string:

return val ? val.name : "N/A"; 

But if the val.name returns "", it should return "N/A", not val.name. How to do that?

I tried

return val ? val.name === "" : "N/A"; return val.name ? val.name : "N/A"; etc. 

But no luck yet.

 


Check whether val.name is truthy as well (an empty string is falsy), using the Boolean AND operator:

val && val.name ? val.name : "N/A" 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: