检查字符串0,0,空字符串,null

时间:2021-01-24 11:40:13

in PHP:

在PHP中:

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

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

验证$var是否为0或“0”或“”或NULL

if (!$var) {...}

in jQuery/JavaScript:

在jQuery和JavaScript:

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

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

if (!$var)适用于除“0”之外的所有值

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

JavaScript/jQuery中是否有一个通用的方法来检查所有类型的空/空/零值,就像php一样?

6 个解决方案

#1


6  

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

JavaScript/jQuery中是否有一个通用的方法来检查所有类型的空/空/零值,就像php一样?

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

不。在PHP中,转换为布尔值的值会产生与JavaScript不同的结果。你不能像PHP那样做。

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') { }

#2


4  

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

抽象操作ToBoolean根据表11将其参数转换为Boolean类型的值:

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、-0或NaN,则结果为false;否则结果就是真的。如果参数是空字符串(其长度为0),则结果为false;否则结果就是真的。真正的对象

0 will return false.

0将返回false。

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

http://www.ecma - international.org/publications/files/ecma st/ecma - 262. - pdf

#3


1  

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

如果你觉得funky(只是在你的具体案例样本中),你可以这样做:(否则未定义,NaN将会导致错误)

+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
});

#4


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.

虽然“0”不是一个空字符串,因此它的值为true,但其余的返回为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.

另外,我同意dfsq的观点,你不应该依赖这个。您应该知道并理解您的变量所持有的类型。

#5


0  

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:

第一个0不是假的,它是真的。因为0是一个字符串。如果字符串存在,返回true。所以!”应该返回false。也就是说,如果你的数据以字符串形式返回0,你可以使用:

parseInt("0", 10);

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

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:

这将返回整数值或NaN。!NaN将返回true,如果它是一个数字…它会返回,所以你可以这样做:

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

#6


0  

Number(variable) seems to produce expected output

数字(变量)似乎产生了预期的输出

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

#1


6  

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

JavaScript/jQuery中是否有一个通用的方法来检查所有类型的空/空/零值,就像php一样?

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

不。在PHP中,转换为布尔值的值会产生与JavaScript不同的结果。你不能像PHP那样做。

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') { }

#2


4  

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

抽象操作ToBoolean根据表11将其参数转换为Boolean类型的值:

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、-0或NaN,则结果为false;否则结果就是真的。如果参数是空字符串(其长度为0),则结果为false;否则结果就是真的。真正的对象

0 will return false.

0将返回false。

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

http://www.ecma - international.org/publications/files/ecma st/ecma - 262. - pdf

#3


1  

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

如果你觉得funky(只是在你的具体案例样本中),你可以这样做:(否则未定义,NaN将会导致错误)

+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
});

#4


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.

虽然“0”不是一个空字符串,因此它的值为true,但其余的返回为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.

另外,我同意dfsq的观点,你不应该依赖这个。您应该知道并理解您的变量所持有的类型。

#5


0  

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:

第一个0不是假的,它是真的。因为0是一个字符串。如果字符串存在,返回true。所以!”应该返回false。也就是说,如果你的数据以字符串形式返回0,你可以使用:

parseInt("0", 10);

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

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:

这将返回整数值或NaN。!NaN将返回true,如果它是一个数字…它会返回,所以你可以这样做:

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

#6


0  

Number(variable) seems to produce expected output

数字(变量)似乎产生了预期的输出

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