iFocus.Life News News - Breaking News & Top Stories - Latest World, US & Local News,Get the latest news, exclusives, sport, celebrities, showbiz, politics, business and lifestyle from The iFocus.Life,

Explicitly Testing Undefined

106 11
When you need your code to text if something exists (either because the preceding code may or may not have created it or in order to do feature sensing to test if the browser supports something natively) then you code an if statement to perform that test for you.

Most commonly the if statement just tests the name of the object that you want to check such as using if (document.getElementById) to test if the getElementById method exists on the document object.

What is not so obvious in doing this is that there are some instances where you might get a false negative. This is because whatever value that is returned needs to be converted into true or false in order to evaluate the if statement and there are some circumstances where what you are testing for exists but returns a value that is converted to false.

Now we need to be careful in determining just what it is that we are asking for with our particular test because while document.getElementById() returns a node list, document.getElementById without the parentheses returns the getElementById object itself.

In the case of feature sensing for native supports for things like getElementById our test works because any object that is returned will evaluate as true. If the object doesn't exist then the call will return null and that evaluates to false.

Where then would we get a false negative? well that really does depend on both the type of element you are testing for and what values it can return if it exists.

Let's consider the situation where we are testing whether the previous code has or hasn't created a particular variable. Now if this valiable is called x then you might expect to be able to test if (x) however that will return a false negative in a variety of situations. The problem is that if x can have any value once it is created then some of those values will evaluate to false and so our simple if statement will incorrectly identify that the variable hasn't been defined. (Our feature sensing tests only work like that because they always return an object if they exist and an object always evaluates to true).

Some examples of how we could define x and still have it fail the if test are:

var x = 0;
var x = '';
var x = false;


Just using x in the if statement itself like that creates a variable called x if one doesn't exist although the value in that case is set to null which still evaluates as false. However in that case our if statement itself has actually created the very thing we are trying to test for.

The only way around this situation so as to avoid the false negatives is to more explicitly test if the variable exists in a way that doesn't result in the creation of the variable by the text itself. To do this we use:

if (typeof x=="undefined")


Only by writing our code this way do we avoid actually creating x if it doesn't already exist and do we avoid any false negatives if the value of x happens to be one that evaluates to false.
Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time
You might also like on "Technology"

Leave A Reply

Your email address will not be published.