I'm trying to use the syntax of:
someVar = otherVar || '';
// set someVar to otherVar, or '' if otherVar is false
when I turn otherVar into some array key,
someVar = otherVar[1] || ''; // otherVar[1] is undefined.
i get the error
Cannot read property '1' of undefined
This makes sense since otherVar[1] is undefined...But -
Question: Is the only way to prevent this, to check if otherVar[1] is truthy before setting someVar? Or can I still use this easy method to do set variables quickly like an if else?
I also tried
someVar = (!!otherVar[1]) ? otherVar[1] : ''; // didn't work either.
Thanks!