I have the following line in my code:
var cart = $("#dynamo_shop_window .dynamo_content tbody .shop_cart").html();
However, I want the value of cart to be an empty string if there are no matching elements on the page, i.e:
!$("#dynamo_shop_window .dynamo_content tbody .shop_cart").size();
If this is the case, var cart = null;, well according to Chrome’s developer tools anyway.
To give it the empty string value, is there any reason why I should use cart = cart !== null ? cart : ''; after the above code instead of replacing the above code with:
var cart = $("#dynamo_shop_window .dynamo_content tbody .shop_cart").html() || '';
The .html() will never return 0 or any other false related statements.
No, not really. The double pipe operator falls through to the right-hand side whenever there’s a falsey value on the left-hand side, so one of
undefined,null,NaN,0or"".If, in all those cases, you want
cartto be"": go for it. Use||.