tinmat Posted May 11, 2010 Share Posted May 11, 2010 Hello,I know that the common way to check whether a certain element/object exists is simply to use a statement like this:if (element) {code} // to perform a task if an element existsif (!element) {code} // to perform a task if an element does not existHowever when I do this - it wont execute/cause an error:if (element==true) // if existsif (element==false) // if doesnt existI ponder why the last method won't work using boolean values. I would be very grateful if anyone could explain me the reasons behind.(I aim to use boolean values on the grounds that it is a lot easier to read/code about) Link to comment Share on other sites More sharing options...
chibineku Posted May 11, 2010 Share Posted May 11, 2010 Think about the possible return values of, say, document.getElementById, are:a reference to the elementnull if it can't be foundIf statements will equate not-null with true, and so will work as expected. But comparing an object reference to the boolean true doesn't work. Link to comment Share on other sites More sharing options...
ShadowMage Posted May 11, 2010 Share Posted May 11, 2010 (I aim to use boolean values on the grounds that it is a lot easier to read/code about)I think I have to disagree. If you ask me it's a lot easier to read and understand what is meant by this:if (document.getElementById("elID")) {than it is this:if (document.getElementById("elID") == true) {The latter would confuse me because I think of the != or == operators as testing the value of something, which is not what you're trying to do. Link to comment Share on other sites More sharing options...
tinmat Posted May 11, 2010 Author Share Posted May 11, 2010 Think about the possible return values of, say, document.getElementById, are:a reference to the elementnull if it can't be foundIf statements will equate not-null with true, and so will work as expected. But comparing an object reference to the boolean true doesn't work.Yeah, it didn't cross my mind to look at the document.getElementById as a method that either returns a reference or a null. "document.getElementById" delivers a return regardless of any event in the document, that is if it been grounded otherwise - could terminate - you could verify whether it failed or reached my statement or not by comparing it to either true or false - and in case it didn't it would execute my statements. While it does not work like this, comparing it to false/true does not make sense. Am I thinking right?Regardless, thanks for your input (appreciated). Gonna play with "!=null / ==null" from now on. Link to comment Share on other sites More sharing options...
justsomeguy Posted May 11, 2010 Share Posted May 11, 2010 You can also use the typeof operator: var el = document.getElementById('some-element');if ((typeof el) == 'undefined'){ alert('not defined');} If you're going for clarity in the code, that's about as clear as you can get. Just make sure you get the parens right, this won't work:if (typeof el == 'undefined')Because of operator precedence rules, that is the same thing as this:if (typeof (el == 'undefined')) Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.