我想知道JavaScript中null和undefined之间的区别。
当前回答
tl;博士
使用null设置一个已知为Object的变量。
使用undefined设置类型混合的变量。
这是我对5个原语和Object类型的用法,这解释了未定义或空的«用例»之间的区别。
一串
如果您知道一个变量在整个生命周期中只是一个字符串,那么按照惯例,您可以将其初始化为“”:
("") ? true : false; // false
typeof ""; // "string";
("Hello World") ? true : false; // true
typeof "Hello World"; // "string"
数字
如果您知道一个变量在所有生命周期中都只是一个数字,那么按照惯例,您可以将其初始化为0(如果0在您的使用中是一个重要值,则为NaN):
(0) ? true : false; // false
typeof 0; // "number";
(16) ? true : false; // true
typeof 16; // "number"
or
(NaN) ? true : false; // false
typeof NaN; // "number";
(16) ? true : false; // true
typeof 16; // "number"
布尔型
如果您知道一个变量只是一个布尔值,而所有生命周期,按照惯例,您可以将其初始化为false:
(false) ? true : false; // false
typeof false; // "boolean";
(true) ? true : false; // true
typeof true; // "boolean"
对象
如果您知道一个变量在所有生命周期中都只是一个Object,那么按照惯例,您可以将其初始化为null:
(null) ? true : false; // false
typeof null; // "object";
({}) ? true : false; // true
typeof {}; // "object"
注意:关闭null的智能用法是Object的错误版本,因为Object始终为true,因为typeof null返回对象。这意味着typeof myVarObject返回Object和null类型的一致值。
All
如果您知道一个变量有一个混合类型(所有生命周期中的任何类型),按照惯例,您可以将其初始化为undefined。
其他回答
Null始终是内存中存在的未知对象,而undefined则不是。
null是一个特殊的关键字,表示缺少值。
将其视为一种价值,例如:
“foo”是字符串,true为布尔值,1234是数字,null未定义。
undefined属性表示尚未为变量分配包含null的值。喜欢
var foo;
定义的空变量为null,数据类型为undefined
它们都表示一个没有值的变量的值
以及null不表示没有值的字符串-空字符串-
Like
var a = '';
console.log(typeof a); // string
console.log(a == null); //false
console.log(a == undefined); // false
现在如果
var a;
console.log(a == null); //true
console.log(a == undefined); //true
BUT
var a;
console.log(a === null); //false
console.log(a === undefined); // true
所以每个人都有自己的使用方法
undefined使用它来比较变量数据类型
null使用它来清空变量的值
var a = 'javascript';
a = null ; // will change the type of variable "a" from string to object
null和undefined是两种不同的对象类型,它们具有以下共同点:
两者都只能保存一个值,分别为null和undefined;两者都没有财产或方法,尝试读取其中一个的任何财产都会导致运行时错误(对于所有其他对象,如果尝试读取不存在的属性,则会得到未定义的值);通过==和!=,值null和undefined被视为彼此相等,其他值都不相等操作员。
然而,相似之处到此为止。这一次,在实现关键字null和undefined的方式上有一个根本的区别。这并不明显,但请考虑以下示例:
var undefined = "foo";
WScript.Echo(undefined); // This will print: foo
undefined、NaN和Infinity只是预初始化的“超级全局”变量的名称-它们在运行时被初始化,可以被具有相同名称的普通全局或局部变量覆盖。
现在,让我们对null做同样的尝试:
var null = "foo"; // This will cause a compile-time error
WScript.Echo(null);
哎呀!null、true和false是保留关键字-编译器不允许将它们用作变量或属性名称
另一个区别是undefined是一种基本类型,而null是一种对象类型(表示没有对象引用)。考虑以下事项:
WScript.Echo(typeof false); // Will print: boolean
WScript.Echo(typeof 0); // Will print: number
WScript.Echo(typeof ""); // Will print: string
WScript.Echo(typeof {}); // Will print: object
WScript.Echo(typeof undefined); // Will print: undefined
WScript.Echo(typeof null); // (!!!) Will print: object
此外,在数字上下文中处理null和undefined的方式也有一个重要的区别:
var a; // declared but uninitialized variables hold the value undefined
WScript.Echo(a === undefined); // Prints: -1
var b = null; // the value null must be explicitly assigned
WScript.Echo(b === null); // Prints: -1
WScript.Echo(a == b); // Prints: -1 (as expected)
WScript.Echo(a >= b); // Prints: 0 (WTF!?)
WScript.Echo(a >= a); // Prints: 0 (!!!???)
WScript.Echo(isNaN(a)); // Prints: -1 (a evaluates to NaN!)
WScript.Echo(1*a); // Prints: -1.#IND (in Echo output this means NaN)
WScript.Echo(b >= b); // Prints: -1 (as expected)
WScript.Echo(isNaN(b)); // Prints: 0 (b evaluates to a valid number)
WScript.Echo(1*b); // Prints: 0 (b evaluates to 0)
WScript.Echo(a >= 0 && a <= 0); // Prints: 0 (as expected)
WScript.Echo(a == 0); // Prints: 0 (as expected)
WScript.Echo(b >= 0 && b <= 0); // Prints: -1 (as expected)
WScript.Echo(b == 0); // Prints: 0 (!!!)
当在算术表达式或数字比较中使用null时,null变为0-与false类似,它基本上只是一种特殊的“零”。另一方面,undefined是一个真正的“无”,当您尝试在数字上下文中使用它时,它会变成NaN(“非数字”)。
注意,null和undefined从==和!=接收特殊处理运算符,但可以使用表达式(a>=b&&a<=b)测试a和b的真正数值相等性。
null:缺少变量值;undefined:缺少变量本身;
..,其中变量是与值关联的符号名称。
JS可以用null隐式初始化新声明的变量,但它没有。
请仔细阅读以下内容。它应该可以消除您对JavaScript中null和undefined之间区别的所有疑虑。此外,您可以在答案末尾使用效用函数来获取更具体的变量类型。
在JavaScript中,我们可以有以下类型的变量:
未声明的变量已声明但未分配的变量未定义文字赋值的变量赋值为文本null的变量变量分配了除未定义或null以外的任何值
以下逐一解释了每种情况:
未声明的变量只能使用返回字符串“undefined”的typeof运算符进行检查不能用松散相等运算符(==undefined)检查,更不用说严格相等运算符(===undefineed),以及if语句和三元运算符(?:)-这些抛出引用错误已声明但未分配的变量typeof返回字符串“undefined”==检查null返回true==未定义的检查返回true==检查null返回false==未定义的检查返回trueif语句和三元运算符(?:)是否错误未定义文字赋值的变量这些变量与已声明但未分配的变量完全相同。赋值为文本null的变量typeof返回字符串“object”==检查null返回true==未定义的检查返回true==检查null返回true==检查未定义返回falseif语句和三元运算符(?:)是否错误变量分配了除未定义或null以外的任何值typeof返回以下字符串之一:“bigint”、“boolean”、“function”、“number”、“object”、“string”、“symbol”
以下提供了正确检查变量类型的算法:
获取变量的类型,如果它不是“object”,则返回它检查null,因为typeof null也返回“object”使用switch语句对Object.protype.toString.call(o)求值,以返回更精确的值。Object的toString方法为本机/主机对象返回类似“[Object ConstructorName]”的字符串。对于所有其他对象(用户定义的对象),它始终返回“[object object]”如果最后一部分是这种情况(变量的字符串化版本为“[objectObject]”),并且参数returnConstructorBoolean为真,则它将尝试通过对其进行字符串化并从中提取名称来获取构造函数的名称。如果无法访问构造函数,将照常返回“object”。如果字符串不包含其名称,则返回“匿名”
(支持ECMAScript 2020之前的所有类型)
function TypeOf(o, returnConstructorBoolean) {
const type = typeof o
if (type !== 'object') return type
if (o === null) return 'null'
const toString = Object.prototype.toString.call(o)
switch (toString) {
// Value types: 6
case '[object BigInt]': return 'bigint'
case '[object Boolean]': return 'boolean'
case '[object Date]': return 'date'
case '[object Number]': return 'number'
case '[object String]': return 'string'
case '[object Symbol]': return 'symbol'
// Error types: 7
case '[object Error]': return 'error'
case '[object EvalError]': return 'evalerror'
case '[object RangeError]': return 'rangeerror'
case '[object ReferenceError]': return 'referenceerror'
case '[object SyntaxError]': return 'syntaxerror'
case '[object TypeError]': return 'typeerror'
case '[object URIError]': return 'urierror'
// Indexed Collection and Helper types: 13
case '[object Array]': return 'array'
case '[object Int8Array]': return 'int8array'
case '[object Uint8Array]': return 'uint8array'
case '[object Uint8ClampedArray]': return 'uint8clampedarray'
case '[object Int16Array]': return 'int16array'
case '[object Uint16Array]': return 'uint16array'
case '[object Int32Array]': return 'int32array'
case '[object Uint32Array]': return 'uint32array'
case '[object Float32Array]': return 'float32array'
case '[object Float64Array]': return 'float64array'
case '[object ArrayBuffer]': return 'arraybuffer'
case '[object SharedArrayBuffer]': return 'sharedarraybuffer'
case '[object DataView]': return 'dataview'
// Keyed Collection types: 2
case '[object Map]': return 'map'
case '[object WeakMap]': return 'weakmap'
// Set types: 2
case '[object Set]': return 'set'
case '[object WeakSet]': return 'weakset'
// Operation types: 3
case '[object RegExp]': return 'regexp'
case '[object Proxy]': return 'proxy'
case '[object Promise]': return 'promise'
// Plain objects
case '[object Object]':
if (!returnConstructorBoolean)
return type
const _prototype = Object.getPrototypeOf(o)
if (!_prototype)
return type
const _constructor = _prototype.constructor
if (!_constructor)
return type
const matches = Function.prototype.toString.call(_constructor).match(/^function\s*([^\s(]+)/)
return matches ? matches[1] : 'anonymous'
default: return toString.split(' ')[1].slice(0, -1)
}
}