javascriptjqueryundefined

jQuery / Javascript code check, if not undefined


Is this code good?

var wlocation = $(this).closest('.myclass').find('li a').attr('href');
if (wlocation.prop !== undefined) { window.location = wlocation; }

or should I do

var wlocation = $(this).closest('.myclass').find('li a').attr('href');
if (wlocation.prop !== "undefined") { window.location = wlocation; }

Solution

  • I like this:

    if (wlocation !== undefined)
    

    But if you prefer the second way wouldn't be as you posted. It would be:

    if (typeof wlocation  !== "undefined")