I have this variable :
var foobar = "Hi, my name is #{name}";
But if name is not defined at load of the page.. I'd like it to save "unknown" instead.
But writing something like this :
var foobar = "Hi, my name is #{typeof name === 'undefined' ? 'unknown' : name}";
Still returns the error, unknown variable name
Instead of making logic decisions within a string construct, do it outside for better performance and (far) more readable code:
name = name||'unknown';
I think a better method would be:
var name = name || "Unknown";
var foobar = 'Hi my name is ' + name;
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