I am using ternary expression to check if the variable exists to display value accordingly, but since, I need to check both if it exists and that is has some values in it, it fails on checking the condition when the variable is undefined. How can I fix that?
This is the code:
$('#select').selectize({
items: typeof icoop != "undefined" && icoop != null || icoop.selectedUsers.length < 1 ? icoop.selectedUsers : []
});
I get:
Uncaught ReferenceError: icoop is not defined
typeof icoop != "undefined"is false, which makes the entire AND condition also false but then you are left with, essentially,false || ( icoop.selectedUsers.length < 1 ? icoop.selectedUsers : [] )and because this is an OR, the evaluation will continue on the right, where the error occurs.