Why do the following expanded if shorthand statements not work in javascript?
Posted
by
PeanutsMonkey
on Stack Overflow
See other posts from Stack Overflow
or by PeanutsMonkey
Published on 2012-06-26T00:58:27Z
Indexed on
2012/06/26
3:16 UTC
Read the original article
Hit count: 219
JavaScript
This is my first attempt to write shorthand if statements however am befuddled by why the expanded versions don't work quite the way I imagined they would.
Code 1 - Does not work
if(document.getElementById == true) {
alert("The document object model is supported by: " + navigator.appName);
}
Code 2 - Does work
if(document.getElementById != false) {
alert("The document object model is supported by: " + navigator.appName);
}
Code 3 - The shorthand that does work
if(document.getElementById) {
alert("The document object model is supported by: " + navigator.appName);
}
Why is that if I expand the shorthand in 3 to the first code sample not work and why does it work if I have it equal to != false
?
© Stack Overflow or respective owner