开发者

What would be a complete way to check for null cross browser

开发者 https://www.devze.com 2023-01-07 21:37 出处:网络
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:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消