Current Status

Actively playing with all possible backend services

23 July, 2009

Javascript how to check variable is null?

do you have idea of what's the most reliable way to check a javascript null?
what does it mean by undefined vs. null in JavaScript? what undefined vs. null?
How found this when i search about "how to check 'null' Value in Java Script?"

if (myVar == null) { alert('myVar is not set.'); }

have you try this?

What?! This isn't why you don't check for equality with null. It's because myVar == null doesn't even remotely do what most people think it does in this context.

A property, when it has no definition, is undefined. Put that way, it's pretty obvious.

'null' is an object. It's type is 'null'. 'undefined' is not an object, it's type is 'undefined'. That part is less obvious.

So people write

if (myVar == null) {
myVar = "Joe";
}

When what they really mean is

if (!myVar) {
myVar = "Joe";
}

this is where i end up with my search... hope you expert guys would help me if i'm wrong.

No comments:

Some Popular Posts