Nishant Jani
Nishant Jani

Reputation: 1993

Dealing with Null in javascript

I'm facing some difficulty when dealing with null values in JavaScript. I have two cases in my JavaScript where the object can be null or can have some other value so I do this:

if(feild_values != null || typeof(feild_values) != 'null') {
    alert(feild_values.id[i-1]);
}            

However, Firebug gives me an error saying:

TypeError: feild_values is null alert(feild_values.id[i-1]);

How do I manage this?

Upvotes: 0

Views: 115

Answers (2)

Yevgeny Simkin
Yevgeny Simkin

Reputation: 28349

you should also be looking for typeof yourVar == "undefined"

Upvotes: 0

Steve Bazyl
Steve Bazyl

Reputation: 11672

Remove || typeof(feild_values) != 'null'

You don't need it and typeof(null) isn't 'null', its 'object'

Can simplify to:

if (feild_values) {
 ...
}

Upvotes: 4

Related Questions