I'm a super green developer that started my first job last week. I got a comment from another developer to add a condition to check if array is undefined and to check if array is in fact an array.
Initially I used:
$: hasActiveCoupons = $cart.coupons && $cart.coupons.length > 0;
if (hasActiveCoupons && $cart.coupons.find((coupon) => coupon.id === couponCode)) {
...
}
But then she pointed out to me that $cart.coupons.length > 0 would return a falsey value. And corrected it by writing:
if ($cart.coupons?.length && $cart.coupons.find((coupon) => coupon.id === couponCode)) {
...
}
So I am wondering how this works? I tired reading a few other questions but I don't fully grasp this concept.