views:

51

answers:

2

I always (thing != undefined || thing != null)?...:...; check. Is there any method will return bool after this check in javascript or jquery ?

And how would you add this check in jquery as a function?

+4  A: 
if (thing)
{
   //your code
}

Is that what you are looking for?

femseks
:) this is really very easy isn't it :D
uzay95
Be careful - this string and maybe a few others will fail that check: `"0"`.
Joel Coehoorn
He didnt ask for "0" and "false", but undefined or null.
femseks
@Joel: Wrong. **All** non-empty strings are truthy.
SLaks
@SLaks But only when no comparison is involved, so `if("0")` will be true but `if("0" == true)` won't, since type conversion took place.
Ivo Wetzel
+4  A: 

In Javascript, the values null, undefined, "", 0, NaN, and false are all "falsy" and will fail a conditional.
All other values are "truthy" and will pass a conditional.

Therefore, you can simply write thing ? ... : ....

SLaks