是否有一种通用的JavaScript函数,可以检查变量是否具有值,并确保它不是undefined
或null
?我有这段代码,但我不确定它是否涵盖了所有情况:
function isEmpty(val){
return (val === undefined || val == null || val.length <= 0) ? true : false;
}
是否有一种通用的JavaScript函数,可以检查变量是否具有值,并确保它不是undefined
或null
?我有这段代码,但我不确定它是否涵盖了所有情况:
function isEmpty(val){
return (val === undefined || val == null || val.length <= 0) ? true : false;
}
这是我检查数据是否为空的解决方案。
const _isEmpty = (data) => {
return (
// this way we can also check for undefined values. null==undefined is true
data == null ||
data == "" ||
(Array.isArray(data) && data.length === 0) ||
// we want {} to be false. we cannot use !! because !!{} turns to be true
// !!{}=true and !!{name:"yilmaz"}=true. !! does not work ofr objects
(data.constructor === Object && Object.keys(data).length === 0)
);
};
仅在值为undefined
或null
时返回false
:
return value ?? false
false ?? false === false
- Pi Delport!(0 ?? false) === true
。 - bnielandfunction notEmpty(value){
return (typeof value !== 'undefined' && value.trim().length);
}
它还将检查空格(' ')以及以下内容:
这将检查未确定嵌套的变量是否未定义
function Undef(str)
{
var ary = str.split('.');
var w = window;
for (i in ary) {
try { if (typeof(w = w[ary[i]]) === "undefined") return true; }
catch(e) { return true; }
}
return false;
}
if (!Undef("google.translate.TranslateElement")) {
if (!(typeof google === "undefined"
|| typeof google.translate === "undefined"
|| typeof google.translate.TranslateElement === "undefined")) {
undefined
或null
。var a=undefined, b=null, c='hello world', d;
if(a !== (a ?? {})) { /**/ } // true
if(b !== (b ?? {})) { /**/ } // true
if(c !== (c ?? {})) { /**/ } // false
if(d !== (d ?? {})) { /**/ } // true
检查变量是否不是 undefined
或 null
var a=undefined, b=null, c='hello world', d;
if(a === (a ?? {})) { /**/ } // false
if(b === (b ?? {})) { /**/ } // false
if(c === (c ?? {})) { /**/ } // true
if(d === (d ?? {})) { /**/ } // false
function isEmpty(value) {
return (
// Null or undefined.
(value == null) ||
// Check if a Set() or Map() is empty
(value.size === 0) ||
// NaN - The only JavaScript value that is unequal to itself.
(value !== value) ||
// Length is zero && it's not a function.
(value.length === 0 && typeof value !== "function") ||
// Is an Object && has no keys.
(value.constructor === Object && Object.keys(value).length === 0)
)
}
返回:
undefined
, null
, ""
, []
, {}
, NaN
, new Set()
, //
true
, false
, 1
, 0
, -1
, "foo"
, [1, 2, 3]
, { foo: 1 }
, function () {}
您可以直接使用等号运算符
<script>
var firstName;
var lastName = null;
/* Since null == undefined is true, the following statements will catch both null and undefined */
if(firstName == null){
alert('Variable "firstName" is undefined.');
}
if(lastName == null){
alert('Variable "lastName" is null.');
}
</script>
try{
let vari = obj.propTest; // obj may be don't have propTest property
...
} catch(NullException){
// do something here
}
我认为使用try catch可以避免任何空值检查错误,在Angular或JavaScript中只需捕获空异常并进行处理。
我认为使用 ? 运算符会更加简洁。
var ? function_if_exists() : function_if_doesnt_exist();
(truthy statement) ? true : false;
。直接写成(truthy statement);
即可。 - David Baucumif (hungry) …
而不是if (hungry === true) …
。就像所有编码风格一样,这只是个人审美问题。更具体地说,对于 OP 提供的例子,他更冗长地表达了“如果它为真,则为真,如果不为真,则为假”,但如果它为真,则已经是真的。如果它为假,则已经是假的。这相当于说:“如果你饿了,那么你就饿了,如果不饿,那么你就不饿。” - David Baucum