I was doing some javascript coding in chrome's console.
我在Chrome的控制台上编写了一些javascript代码。
When I do:
当我这样做的时候:
console.log(x);
let x;
It gives "ReferenceError: x is not defined"
它给出“ReferenceError:未定义X”
But when I run the below code:
但当我运行以下代码时:
let x;
console.log(x);
It consoles "undefined".
它让人联想到“未定义的”。
Q1: Why is it happening?
Q1:为什么会这样?
Q2: When I ran the first code in an online js compiler called OneCompiler, it didn't give the same Reference error but gave "Reference error : cannot access x before initialization". Why did the two compilers gave different errors?
Q2:当我在一个名为OneCompiler的在线js编译器中运行第一个代码时,它没有给出相同的引用错误,但给出了“引用错误:在初始化之前无法访问x”。为什么两个编译器会给出不同的错误?
更多回答
The browser console is not a compiler. The firsst snippet tries to access a variable which isn't known yet. The second code accesses a known variable which doesn't have a value yet
浏览器控制台不是编译器。第一个代码片段试图访问一个未知的变量。第二个代码访问一个尚未赋值的已知变量
Could you give a link to the OneCompiler snippet you are talking about? I tried both the "Javascript" and "NodeJS" options, and they lead to the same ReferenceError
.
您能给出一个您正在谈论的OneCompiler代码片段的链接吗?我尝试了“Java脚本”和“NodeJS”选项,它们导致相同的ReferenceError。
@InSync I tried the code in this: onecompiler.com/javascript/3zm4uhwew Yes, they give the same reference error but the message is different, in chrome it says "x is not defined", but in OneCompiler it says "Cannot access 'x' before initialization"
@InSync我尝试了以下代码:onecompiler.com/javascript/3zm4uhwew是的,它们给出了相同的引用错误,但消息不同,在Chrome中它说“x未定义”,但在OneCompiler中它说“在初始化之前无法访问‘x’”。
@derpirscher when I run both snippets with var keywords, it shows undefined in both cases. But, when using let, in both cases I should have got undefined. Please elaborate your comment, I guess Under both sircumstances the let variable should be known.
@derpirscher当我使用var关键字运行这两个代码片段时,在两种情况下都显示为UNDEFINED。但是,在使用let时,在这两种情况下我都应该没有定义。请详细说明您的意见,我想在这两种情况下,let变量都应该是已知的。
In your first code, you call the console.log(x)
before the x
even exists, because you define it first after the console.log(x)
. so there is no reference to an x
.
在您的第一个代码中,您甚至在x存在之前就调用了console.log(X),因为您首先在console.log(X)之后定义它。所以没有提到x。
In your second code, you define the x
and then call the console.log(x)
so your x
already exists and you get no error. But You have not assigned any value to your x
. so x has the value "undefined
". It is always like that in javascript. If you declare a variable without initializing it, the variable has the value "undefined
"
在您的第二段代码中,您定义了x,然后调用console.log(X),这样您的x就已经存在,并且不会出现错误。但您尚未将任何值赋给您的x。因此,x的值为“未定义”。在Java脚本中总是这样的。如果在未初始化的情况下声明变量,则该变量的值为“unfined”
I hope this helps
我希望这能帮到你
This is because of the concept of variable hoisting in javascript. In this code
这是因为在Java脚本中存在变量提升的概念。在此代码中
console.log(x);
let x;
the variable x gets "hoisted" to the top and since you havent declared a value to it you get the error ReferenceError: x is not defined
.
变量x被“提升”到顶部,因为您还没有为它声明一个值,所以会得到错误ReferenceError:x is not fined。
In the second code snippet, your approach is correct but since you have not defined x therefore you get undefined.
在第二个代码片段中,您的方法是正确的,但是因为您没有定义x,所以您没有定义。
Regarding your second question, it is possible that OneCompiler is using a different Javascript engine/implementation hence the difference in error but the underlying cause of the error is the same.
关于你的第二个问题,可能是OneCompiler使用了不同的Java引擎/实现,因此错误有所不同,但导致错误的根本原因是相同的。
更多回答
This is incorrect. The first x
is referenced in the temporal dead zone and is therefore leading to an error. Variables declared with let
(as with const
and class
) are not hoisted at all. Also, the second snippet has no error.
这是不正确的。第一个x在时间死区中被引用,因此导致错误。使用let声明的变量(与const和class一样)根本不会被提升。此外,第二个代码段没有错误。
@InSync in the second snippet why the let variable is not in the temporal dead zone? Please elaborate. I mean we didn't assign any value to them in both snippets, so they should remain in temporal dead zone in both cases and should have shown the same error. Please let me know why this is not the case.
@InSync在第二个片段中为什么let变量不在时间死区?请详细说明。我的意思是,我们在两个代码段中都没有给它们赋值,所以它们在两种情况下都应该保持在时间死区,并且应该显示相同的错误。请让我知道为什么不是这样。
Fixed the undefined error part, that was a typo. Regarding your statement that "Variables declared with let (as with const and class) are not hoisted at all", is incorrect. A simple google search would have sufficed. Source: stackoverflow.com/questions/31219420/… and freecodecamp.org/news/javascript-let-and-const-hoisting
修复了未定义的错误部分,这是一个打字错误。关于您所说的“使用let声明的变量(与const和class一样)根本不被提升”,这是不正确的。简单地在谷歌上搜索一下就足够了。来源:Stackoverflow.com/Questions/31219420/…和freecodecamp.org/news/javascript-let-and-const-hoisting
@Hassoo It depends on what you define "hoisting". The word is not defined by the spec, which means one may interpret it as will; I personally consider ES6 declarations to not be hoisted.
@Hassoo这取决于你对“吊装”的定义。这个词没有在规范中定义,这意味着人们可以随意解释它;我个人认为ES6声明不应该被挂起。
Sorry if that caused some confusion, but I hope that you do get the gist.
如果这造成了一些混乱,我很抱歉,但我希望您确实明白大意。
我是一名优秀的程序员,十分优秀!