Another elegant approach is using logical AND (&&) to guard the access:
return (results && results[1]) || 0;
It returns results[1] if results exists, otherwise 0.
This is a common idiom familiar to many JavaScript developers.
Another elegant approach is using logical AND (&&) to guard the access:
return (results && results[1]) || 0;
It returns results[1] if results exists, otherwise 0.
This is a common idiom familiar to many JavaScript developers.