Javascript Exceptions - testing for true - When is undefined actually undefined?

Javascript has to know what 'it' is before Javascript can tell if 'it' is what you want 'it' to be. Example:
if (unreferencedVariableName) { alert('Impossible! an unreferenced variable cannot return true. In fact it cannot return at all!'); }
Result: ReferenceError: unreferencedVariableName is not defined
/* is as true as the unescapable pain and suffering of being an alive human being  */
if (typeof(cowBoySuckMonkey) === 'undefined') { console.log('suck it!') }


/*   these are false - no if-fu for you!   */
if (typeof(cowBoySuckMonkeee) != 'undefined') { console.log('suck it again!') }
if (!typeof(cowBoySuckMonkeee)) { console.log('suck it for real!') }

/* 'undefined is something I guess - just not what I wanted to test against */
if (typeof(cowBoySuckMonke)) { console.log('suck it nut gobbler!') }

var cowboySuckMonkay = '';
if (typeof(cowboySuckMonkay) != 'undefined') { console.log('suck it empty string!') }
/* no joy */
if (typeof(cowboySuckMonkay)) { console.log('suck it empty string, part duex!') }

/* well, I guess an empty string is false - CaulkGobblin! */
if (cowboySuckMonkay) { console.log('suck it empty string, you still not true!') }

if (cowboySuckMonkay == '') { console.log('suck it empty string, now you true!') }
so much to remember…

Posted by jeremy 18/08/2009 at 15h06


Comments

Leave a response

Leave a comment