What would be a complete way to check for null cross browser
I'm looping through an array in JavaScript to check for null in each object using jQuery, what would be the best cross browser solution for 开发者_高级运维this?
What's wrong with this:
if (myValue === null)
{
\\ Null
}
Null is a reserved keyword in JavaScript, and it shouldn't change across browsers.
null
is pretty reliably null
. If you don't care specifically about null
- that is, if you'd do the same thing when something is undefined
as you would when it's null
or any other "falsy" value, you can just use
if (!array[i]) { /* nothing there */ }
However that's not safe if you're data is numeric, because zero is "falsy", or if they're strings where an empty string should not count as "empty" in the array, for the same reason. Thus you can compare with the double-equals comparator to null
if (array[i] == null) { /* nothing there */ }
I've never heard of any cross-browser issues with this.
(obj == null)
is pretty damn cross browser last time I checked.
精彩评论