设置 JavaScript 变量 = null,还是保持未定义?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16474796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set JavaScript variable = null, or leave undefined?
提问by rgwozdz
When declaring variables at the top of the JavaScript function, is it best practice to set them equal to null, or leave as 'undefined'? Another way to ask, what circumstances call for each option below?
在 JavaScript 函数的顶部声明变量时,最佳实践是将它们设置为 null,还是保留为“未定义”?另一种问法,什么情况下需要下面的每个选项?
Option A:
选项A:
var a = null,
b = null;
Option B:
选项 B:
var a,
b;
采纳答案by Steven Wexler
I declare them as undefined when I don't assign a value because they are undefined after all.
当我不分配值时,我将它们声明为未定义,因为它们毕竟是未定义的。
回答by Khanh TO
It depends on the context.
这取决于上下文。
"undefined" means this value does not exist.
typeof
returns "undefined""null" means this value exists with an empty value. When you use
typeof
to test for "null", you will see that it's an object. Other case when you serialize "null" value to backend server like asp.net mvc, the server will receive "null", but when you serialize "undefined", the server is unlikely to receive a value.
“未定义”表示该值不存在。
typeof
返回“未定义”“null”表示此值存在且值为空。当您
typeof
用来测试“null”时,您会看到它是一个对象。其他情况下,当您将“null”值序列化到后端服务器(如 asp.net mvc)时,服务器将收到“null”,但是当您将“undefined”序列化时,服务器不太可能接收到值。
回答by KennyC
Generally, I use null for values that I know can have a "null" state; for example
通常,我将 null 用于我知道可以具有“空”状态的值;例如
if(jane.isManager == false){
jane.employees = null
}
Otherwise, if its a variable or function that's not defined yet (and thus, is not "usable" at the moment) but is supposed to be setup later, I usually leave it undefined.
否则,如果它是一个尚未定义的变量或函数(因此,目前不“可用”)但应该稍后设置,我通常不定义它。
回答by Vishal Sakaria
Generally speak I defined null is indicate a human set the value and undefined to indicate no setting has taken place.
一般来说,我定义的 null 表示有人设置了该值,而 undefined 表示没有设置。
回答by snirad
I usually set it to whatever I expect to be returned from the function.
我通常将它设置为我期望从函数返回的任何内容。
If a string, than i will set it to an empty string ='', same for object ={} and array=[], integers = 0.
如果是字符串,我会将其设置为空字符串 ='',对象 ={} 和数组 = [] 相同,整数 = 0。
using this method saves me the need to check for null / undefined. my function will know how to handle string/array/object regardless of the result.
使用此方法使我无需检查 null / undefined。无论结果如何,我的函数都将知道如何处理字符串/数组/对象。
回答by barbsan
Be careful if you use this value to assign some object's property and call JSON.stringify
later* - nulls will remain, but undefined properties will be omited, as in example below:
如果您使用此值分配某些对象的属性并JSON.stringify
稍后调用*,请小心- 将保留空值,但将省略未定义的属性,如下例所示:
var a, b = null;
c = {a, b};
console.log(c);
console.log(JSON.stringify(c)) // a omited
*or some utility function/library that works in similar way or uses JSON.stringify
underneath
*或一些以类似方式工作或JSON.stringify
在下面使用的实用程序函数/库
回答by jterry
The only time you need to set it (or not) is if you need to explicitly check that a variable a
is set exactly to null
or undefined
.
您唯一需要设置(或不设置)的时间是您是否需要明确检查变量a
是否完全设置为null
或undefined
。
if(a === null) {
}
...is not the same as:
...与以下不同:
if(a === undefined) {
}
That said, a == null && a == undefined
will return true
.
也就是说,a == null && a == undefined
会返回true
。
回答by Saloni Malhotra
There are two features of null we should understand:
我们应该了解null的两个特点:
- nullis an empty or non-existent value.
- nullmust be assigned.
- null是空值或不存在的值。
- 必须分配空值。
You can assign null to a variable to denote that currently that variable does not have any value but it will have later on. A null means absence of a value.
您可以为变量分配 null 以表示该变量当前没有任何值,但稍后会有值。null 表示没有值。
example:-
例子:-
let a = null;
console.log(a); //null
回答by Hyman
You can use '';
to declaring NULL variable in Javascript
您可以使用'';
在 Javascript 中声明 NULL 变量