0

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?

1
  • 2
    @arxanas - Indeed, I hope that is an error that will not propagate through a large project. People need to realize how confusing spelling errors can be, especially in a dynamic language like JavaScript. Commented Aug 18, 2012 at 5:19

2 Answers 2

4

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

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

Can simplify to:

if (feild_values) {
 ...
}
Sign up to request clarification or add additional context in comments.

3 Comments

Also, you should use the !== operator.
I'm not sure that this is the best replacement. The code path will fail if feild_values is non-empty string or even Boolean true.
That's for sure, but if your data isn't the right type, you've got a bug somewhere else. There's no need to be paranoid and write super defensive code that hides bugs.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.