JavaScript中是否存在字符串.Empty,还是只是检查“”?


当前回答

检查是否为字符串类型,如果不为空:

const isNonEmptyString = (val) => typeof val === 'string' && !!val

其他回答

试试这个

str.value.length == 0

我通常用这样的东西,

if (!str.length) {
    // Do something
}

试试看:

export const isEmpty = string => (!string || !string.length);

最接近str.Empty(前提是str是String)的是:

if (!str.length) { ...

空字符串,未定义,null。。。

检查真实值:

if (strValue) {
    // strValue was non-empty string, true, 42, Infinity, [], ...
}

要检查错误值,请执行以下操作:

if (!strValue) {
    // strValue was empty string, false, 0, null, undefined, ...
}

空字符串(仅限!)

要检查是否正好为空字符串,请使用==运算符与“”进行严格相等比较:

if (strValue === "") {
    // strValue was empty string
}

要严格检查非空字符串,请使用!==操作员:

if (strValue !== "") {
    // strValue was not an empty string
}