Testing if Something is Undefined in Javascript

Had a question come up from a colleague that used to cause problems when I first started using JavaScript. How to test whether a variable is undefined:

var a = 'defined'
if(typeof a == 'undefined') {
alert('it is undefined');
}

Leave a Reply