This coffeescript:
if typeof(groups) is undefined or groups.length == 0
gets compiled into the following javascript:
if (typeof groups === void 0 || groups.length === 0)
Basically i want to check if the groups array is null or empty and hoping that if the first condition is true, the second condition will not be checked (short-circuiting). However Chrome throws the following error:
Uncaught type error: Cannot read property length of undefined
Any insights on why it wouldnt short-circuit?
CoffeeScript has an operator that takes care of this case for you. If you use:
it will form what you’re looking for, taking care of the undefined/null cases and only trying
groups.lengthifgroupsis defined. This has the advantage of being easily chained: