11

in PHP:

$var=0;
$var="";
$var="0";
$var=NULL;

to verify if $var is 0 or "0" or "" or NULL

if (!$var) {...}

in jQuery/JavaScript:

$var=0;
$var="";
$var="0";
$var=NULL;

if (!$var) works for every value except for "0"

Is there a general way in JavaScript/jQuery to check all kinds of those empty/null/zero values, exactly like php does?

3
  • Don't rely on this confusing type castings. Use explicit strict checks with ===. Commented Dec 8, 2014 at 21:14
  • What if the string isn't empty, but only has space characters? If you include those, then you can convert the value to a number. All of those will end up as 0... if (+my_var === 0) { Commented Dec 8, 2014 at 21:25
  • If this is really your case why don't you just check for !value || value == '0'? Commented Dec 8, 2014 at 21:26

6 Answers 6

8

Is there a general way in JavaScript/jQuery to check all kinds of those empty/null/zero values, exactly like php does?

No. In PHP, the values converted to booleans produces different results than in JavaScript. So you can't do it exactly like PHP does.

Why not be (a bit more) explicitly about it which makes your code easier to understand?

// falsy value (null, undefined, 0, "", false, NaN) OR "0"
if (!val || val === '0') { }
Sign up to request clarification or add additional context in comments.

3 Comments

I first answered !+v but it's not reliable the other way around: example
Felix: based on your answer:function is_empty(myvar) {var res=false; if (!myvar || myvar==='0') res=true; return res; }
This solution with === does not provide sufficient coverage. if val = "00", it will return true instead of false.
6

Number(variable) seems to produce expected output

Number(0) = 0
Number("0") = 0
Number("") = 0
Number(null) = 0
Number(false) = 0

Comments

4

The abstract operation ToBoolean converts its argument to a value of type Boolean according to Table 11:

Undefined false
Null false
Boolean The result equals the input argument (no conversion).
Number The result is false if the argument is +0, -0, or NaN; otherwise the result is true.
String The result is false if the argument is the empty String (its length is zero);
otherwise the result is true.
Object true

0 will return false.

http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf

2 Comments

thanks, I noticed that, but I am looking for a general way to check it in JS like php does.
the way you did in php.
3

If you feel funky (just in your exact case-sample) you can do this:
(otherwise undefined and NaN will result as false)

+v===0

Example:

var a = [0, "",  "0", null];
var b = [1, "a", "1", {}];

a.forEach(function(v){
  console.log( +v===0 ); // true, true, true, true
});

b.forEach(function(v){
  console.log( +v===0 ); // false, false, false, false
});

Comments

1

First of "0" isn't false, its true. Because '0' is a string. Strings if they exist return true. So !"" should return false. That said if your data is returning zeros as strings you can use:

parseInt("0", 10);

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt

This will return the integer value or NaN. !NaN will return true, and if it is an number of say... 0 it will return !0 so you could do this:

function nullOrEmpty(value) {
    return (!value && !parseInt(value));
}

2 Comments

parseInt not Parse.Int
"Strings if they exist return true. So !"" should return false." That's not correct. !"" returns true.
0

You could very well use a double negative.

var test = 0;
console.log(!!test);

var test = "";
console.log(!!test);

var test = "0";
console.log(!!test);

var test = null;
console.log(!!test);

Although "0" is not an empty string so it evaluates to true, the rest return false.

Also, I agree with the comment that dfsq made in that you shouldn't rely on this. You should know and understand what types your variables are holding.

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.