Reputation: 14421
Reading through some JavaScript from an ecommerce hosting company and came across this:
function add_wishlist()
{
if ('1' == '1') // <-- What is this?
{
window.open('addtowishlist.asp?itemid=137','popup','height=300,width=550,location=no,scrollbars=no,menubars=no,toolbars=no,resizable=yes');
}
else
{
document.add.action = "add_cart.asp?action=addWishList";
document.add.submit();
}
}
From my basic understanding of JavaScript, the ==
equality operator will attempt type conversion if required then compare the values. In this case, the character 1
compared to the character 1
, which seems like it will always be true.
I feel I might be missing something because why have an else
clause if the code is always unreachable? This appears to have been purposeful code as I can't imagine it's easy to accidentally write this comparison. However, there does appear to be some inherent sloppiness to this code since JavaScript programmers should be in the habit of writing curly braces on the same line to avoid any possible semicolon insertion quirks...
Am I missing something here? Or is it just sloppy code?
Upvotes: 1
Views: 57
Reputation: 78890
Sometimes always true statements are used by developers to temporarily disable a branch of code. IMO, comments are a better way to do this.
Another possibility is that this is code generated by a tool, and this is the emitted code they got when disabling a feature.
Upvotes: 2