Toshi
Toshi

Reputation: 23

Typeof === 'undefined' issue

Newbie here trying to work on this function which is supposed to console.log "I am undefined" if the contents of href is blank. I would appreciate any help as I am VERY stuck... :-(

$(document).ready(function () {
    $('.facebook').each(function () {
        var facebook = $(this).attr('href')
        if (typeof facebook === "undefined") {
            console.log("I am undefined");
        }
    });
});

Upvotes: 1

Views: 330

Answers (2)

Mritunjay
Mritunjay

Reputation: 25882

It won't be undefined, I think you can probably use :-

if (!facebook) {
    console.log("I am undefined");
}

it will take care of any falsy value as null, undefined, "".

Upvotes: 6

Anik Islam Abhi
Anik Islam Abhi

Reputation: 25352

.attr return plain object . it won't return undefined

Try like this

$(document).ready(function () {
    $('.facebook').each(function () {
        var facebook = $(this).attr('href')
        if (!facebook) {
            console.log("I am undefined");
        }
    });
});

Upvotes: 3

Related Questions