user557419
user557419

Reputation:

If-statement condition checking on != "undefined" fails

I'm trying to generate some HTML content for a google maps infowindow. I have 7 values which is supposed to be displayed if they do not equal null, undefined or "" (empty string).

But apparently my if(e.Property != null || e.Property != "undefined" || e.Property == "") doesn't work when a Property is undefined. Mostly the case is that e.Email is undefined. So instead of skipping that part, my code still inserts the html + "<br /> part. And when I alert() the e.Email it returns undefined which it's supposed to catch and skip if that was the case.

I have tried writting if(typeof e.Property != null || typeof e.Property != "undefined" || typeof e.Property == ""), but that made no difference.

// 'e ' is JSON object
var generateHTML = {
    init: function(e) {
        if (e != null || e != "undefined"){
            generateHTML.check(e);
        }
    },
    check: function (e) {
        if(e.Title != null || e.Title != "undefined" || e.Title == ""){
            html = html + "<b>"+e.Title+"</b>";
        }
        if(e.Address != null || e.Address != "undefined" || e.Address == ""){
            html = html +"<br />"+ e.Address;
        }
        if(e.Zipcode != null || e.Zipcode != "undefined" || e.Zipcode == ""){
            html = html +"<br />"+ e.Zipcode+", ";
        }
        if(e.City != null || e.City != "undefined" || e.City == ""){
            html = html + e.City;
        }
        if(e.Phone != null || e.Phone != "undefined" || e.Phone == ""){
            html = html +"<br />"+ e.Phone;
        }
        if(e.Email != null || e.Email != "undefined" || e.Email == ""){
            html = html +"<br />"+ e.Email;
        }
        if(e.WebAddress != null || e.WebAddress != "undefined" || e.WebAddress == ""){
            html = html +"<br />"+ e.WebAddress;
        }
        return html;
    }
};

Upvotes: 7

Views: 41734

Answers (7)

Colin Jameson
Colin Jameson

Reputation: 41

I would also use the length function, if the array or object is empty the Logged length will be 0.0, i.e.

if(e.length == 0){
  //then do something or nothing
}
else {
  //Do somthing
}

Upvotes: 0

vonsko
vonsko

Reputation: 395

better check something via e.length cause variables type are not acurate in JavaScript

Upvotes: 0

Brook
Brook

Reputation: 305

if(e) //this would be shorter

if(e != undefined)
//
if(typeof(e) != 'undefined')

Upvotes: 3

jabclab
jabclab

Reputation: 15042

If you want a more shorthand version you can just use:

if (e.Title) {
    // add to HTML
}
if (e.Address) {
    // add to HTML
}

You may want to consider building your HTML as an Array and then joining at the end to avoid creating many strings, e.g.

var html = [];
html.push("FirstName");
html.push("<br />");
html.push("LastName");
html.push("<br />");
html.push("Number");
var output = html.join(""); // "FirstName<br />LastName<br />Number"

Upvotes: 3

Kemal Can Kara
Kemal Can Kara

Reputation: 416

You are checking it as if its value is string "undefined"

remove the ""

Upvotes: 1

Richard Dalton
Richard Dalton

Reputation: 35793

undefined is a variable name, not a string.

You don't need the quotes around it.

Upvotes: 1

psx
psx

Reputation: 4048

You want to check for !== undefined

e.g.

if(myvar !== undefined) { 
    //DO SOMETHING 
}

Upvotes: 7

Related Questions