Javascript 未定义、空字符串和 if 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9522233/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 06:58:13  来源:igfitidea点击:

Javascript undefined, empty strings, and if statements

javascript

提问by Bltucker

I am not some one who has done alot in javascript. My background is in Java/C++. I am currently working on a project that is using alot of javascript however and I came across something that doesn't make sense to me but hopefully some javascript guru out there can give me a nice logical explanation.

我不是一个在 javascript 中做了很多的人。我的背景是 Java/C++。我目前正在开发一个使用大量 javascript 的项目,但是我遇到了一些对我来说没有意义的事情,但希望那里的一些 javascript 专家可以给我一个很好的合乎逻辑的解释。

var noDefinition = undefined;
var emptyString = "";
var noDefinitionAndEmptyString = noDefinition + emptyString;

console.log("NoDefinition");
console.log(noDefinition);

console.log("EmptyString");
console.log(emptyString);

console.log("noDefinition+emptyString");
console.log(noDefinitionAndEmptyString);

console.log("************************");
if(noDefinition == undefined)
{
    console.log("No Definition is undefined");
}


if(emptyString == undefined)
{
    console.log("emptyString is undefined");
}

if(noDefinitionAndEmptyString == undefined)
{
    console.log("noDefiniton and emptyString is undefined");
}

The code above produces the following results in my console:
[INFO] NoDefinition
[INFO]
[INFO] EmptyString
[INFO]
[INFO] noDefinition+emptyString
[INFO] undefined
[INFO] ************
[INFO] No Definition is undefined

上面的代码在我的控制台中产生以下结果:
[INFO] NoDefinition
[INFO]
[INFO] EmptyString
[INFO]
[INFO] noDefinition+emptyString
[INFO] undefined
[INFO] *** *** *** ***
[INFO] 没有定义未定义

So as you can see when I output the variables noDefinition and emptyString to the console, it produces blank output. When I concatenate them the console will produce undefined. However if I then proceed to use an if statement and compare each of them to undefined. The only if that executes is the first one.

因此,正如您所看到的,当我将变量 noDefinition 和 emptyString 输出到控制台时,它会产生空白输出。当我连接它们时,控制台将产生 undefined。但是,如果我继续使用 if 语句并将它们中的每一个与 undefined 进行比较。唯一执行的是第一个。

This occurs even though when put to a console the value just shows up blank. Also the concatenation which shows up as undefined in the console fails its compare against undefined and never executes. I am confused by this and I am hoping out there some one can give me an explanation about what is going on.

即使在放入控制台时该值仅显示为空白,也会发生这种情况。此外,在控制台中显示为 undefined 的串联未能与 undefined 进行比较,并且永远不会执行。我对此感到困惑,我希望有人可以向我解释正在发生的事情。

采纳答案by rroche

When you concatenate undefined with a string it becomes a string

当您将 undefined 与一个字符串连接起来时,它就变成了一个字符串

var foo = undefined;
var bar = "";
console.log(foo+bar, typeof(foo+bar));
* output *
undefined string

ECMAScript Docs - http://www.ecmascript.org/docs.phpNot sure but reading the doc on Page 141 you can find this

ECMAScript Docs - http://www.ecmascript.org/docs.php不确定,但阅读第 141 页的文档你可以找到这个

15.5.1 The String Constructor Called as a Function

When String is called as a function rather than as a constructor, it performs a type conversion.

15.5.1 作为函数调用的字符串构造函数

当 String 作为函数而不是构造函数被调用时,它执行类型转换。

Which lets me to believe why concatenation of anything with a string outputs a string

这让我相信为什么用字符串连接任何东西都会输出一个字符串

回答by Rob W

  1. console.logdoesn't print anything when it receives a undefinedvalue (implementation-specific).
  2. When you concatenate undefinedwith an empty string, the resulting value is "undefined". That's printed.
  1. console.log当它收到一个undefined值(特定于实现)时不打印任何内容。
  2. undefined与空字符串连接时,结果值为"undefined"。那是打印出来的。

The previous bullet points explain your observations.

前面的要点解释了您的观察结果。

回答by Juan Carlos Moreno

Rewriting your example might shed more light on what actually happens when concatenating a string and an undefined value:

重写您的示例可能会更清楚地了解连接字符串和未定义值时实际发生的情况:

var noDefinition = undefined;
var emptyString = "";
var noDefinitionAndEmptyString = noDefinition + emptyString;
console.log("************************");
console.log("NoDefinition:" + " " + noDefinition + " " + typeof(noDefinition));

console.log("EmptyString:"+ " " + emptyString + " " + typeof(emptyString));

console.log("noDefinition+emptyString: " + noDefinitionAndEmptyString + " " + typeof(noDefinitionAndEmptyString));

console.log("************************");

Results in:

结果是:

************************
NoDefinition: undefined undefined
EmptyString: string
noDefinition+emptyString: undefined string
************************

回答by Maxim Krizhanovsky

noDefinitionAndEmptyStringis actually a string(you can use console.info(typeof noDefinitionAndEmptyString)) and its value is 'undefined'. This happens because of the concatenation - an implicit cast to stringoccurs, and undefined variable, casted to string, is the string 'undefined'

noDefinitionAndEmptyString实际上是一个string(你可以使用console.info(typeof noDefinitionAndEmptyString)),它的值是'undefined'。发生这种情况是因为连接 -string发生了隐式转换,而未定义的变量,转换为字符串,是字符串 'undefined'

回答by Eric Hodonsky

if(noDefinition == null)covers typeOf 'undefined' and typeOf 'null', but you may want to add an and statement to make sure it's just not an empty string... like so:

if(noDefinition == null)涵盖 typeOf 'undefined' 和 typeOf 'null',但您可能需要添加一个 and 语句以确保它不是空字符串......像这样:

if(noDefinitionAndEmptyString == null || noDefinitionAndEmptyString == "")

if(noDefinitionAndEmptyString == null || noDefinitionAndEmptyString == "")

回答by BPS

Use this instead:

改用这个:

if (typeof noDefinition == "undefined") ...