如何判断变量是undefined
还是null
?
我的代码如下:
var EmpName = $("div#esd-names div#name").attr('class');
if(EmpName == 'undefined'){
// DO SOMETHING
};
<div id="esd-names">
<div id="name"></div>
</div>
但是当我这样做时,JavaScript解释器会停止执行。
如何判断变量是undefined
还是null
?
我的代码如下:
var EmpName = $("div#esd-names div#name").attr('class');
if(EmpName == 'undefined'){
// DO SOMETHING
};
<div id="esd-names">
<div id="name"></div>
</div>
但是当我这样做时,JavaScript解释器会停止执行。
据我所知,在JavaScript中,我们可以像下面这样检查一个未定义的、null或空变量。
if (variable === undefined){
}
if (variable === null){
}
if (variable === ''){
}
检查所有条件:
if(variable === undefined || variable === null || variable === ''){
}
var
is a reserved word, this will throw SyntaxError
- dhilt由于您正在使用 jQuery,因此可以使用一个函数来确定变量是未定义还是其值为 null。
var s; // undefined
jQuery.isEmptyObject(s); // will return true;
s = null; // defined as null
jQuery.isEmptyObject(s); // will return true;
// usage
if(jQuery.isEmptyObject(s)){
alert('Either variable: s is undefined or its value is null');
}else{
alert('variable: s has value ' + s);
}
s = 'something'; // defined with some value
jQuery.isEmptyObject(s); // will return false;
您可以直接使用以下代码(我知道还有更短的方法,但这可能会使视觉上更容易观察,至少对于查看代码的其他人来说)。
if (x === null || x === undefined) {
// Add your response code here, etc.
}
来源:https://www.growthsnippets.com/how-can-i-determine-if-a-variable-is-undefined-or-null/
我刚遇到了这个问题,即如何检查对象是否为空。
我只是使用了这个:
if (object) {
// Your code
}
例如:if (document.getElementById("enterJob")) {
document.getElementById("enterJob").className += ' current';
}
随着最新的JavaScript变化,你可以使用新的逻辑运算符??=
来检查左操作数是否为null
或undefined
,如果是,则将右操作数的值赋给它。
所以,
if(EmpName == null){ // if Variable EmpName null or undefined
EmpName = 'some value';
};
等价于:
EmpName ??= 'some value';
jQuery检查元素是否为null:
var dvElement = $('#dvElement');
if (dvElement.length > 0) {
// Do something
}
else{
// Else do something else
}
最简单的检查方法是:
if(!variable) {
// If the variable is null or undefined then execution of code will enter here.
}
undefined
、null
和一些其他的东西,如空字符串、+0、-0、NaN
和false
都可以通过。!
运算符强制操作数(这里是variable
)转换为布尔值:https://www.ecma-international.org/ecma-262/#sec-toboolean - Alirezafunction check(a) {
const value = a?.valueOf();
if (value === undefined) {
console.log("a is null or undefined");
}
else {
console.log(value);
}
}
check(null);
check(undefined);
check(0);
check("");
check({});
check([]);
顺便提一下,a?.constructor
也可以使用:
function check(a) {
if (a?.constructor === undefined) {
console.log("a is null or undefined");
}
}
check(null);
check(undefined);
check(0);
check("");
check({});
check([]);
更新:我们与StackOverflow的一位成员一起找到了更好的方法。
我个人会这样做:
if (Object.is(a ?? null, null)) {
// a is null or undefined
}
a?.valueOf() === undefined
可靠地工作".... 呃, a = {valueOf() {}}
。 - Константин Ванa = Object.create(null)
,这可能是一件不那么愚蠢但同样无用的事情。它不是人类可能会错误引入的 bug,必须是有意为之。否则,没错,它是 JavaScript,你可以随心所欲地进行黑客攻击。 - noseratio - open to work使用以下解决方案:
const getType = (val) => typeof val === 'undefined' || !val ? null : typeof val;
const isDeepEqual = (a, b) => getType(a) === getType(b);
console.log(isDeepEqual(1, 1)); // true
console.log(isDeepEqual(null, null)); // true
console.log(isDeepEqual([], [])); // true
console.log(isDeepEqual(1, "1")); // false
etc...
export function isEmpty (v) {
if (typeof v === "undefined") {
return true;
}
if (v === null) {
return true;
}
if (typeof v === "object" && Object.keys(v).length === 0) {
return true;
}
if (Array.isArray(v) && v.length === 0) {
return true;
}
if (typeof v === "string" && v.trim().length === 0) {
return true;
}
return false;
}