admin管理员组文章数量:1242848
I have code like this:
let accessAllowed;
accessAllowed = (2>18) ? true : false;
alert(accessAllowed);
However when I use this:
let accessAllowed;
let accessAllowed = (2>18) ? true : false;
alert(accessAllowed);
It results is an error and none of the JavaScript works.
Being a newbie to JS, I’m unsure if this is a feature of let
. I couldn’t find anything about this elsewhere.
I have code like this:
let accessAllowed;
accessAllowed = (2>18) ? true : false;
alert(accessAllowed);
However when I use this:
let accessAllowed;
let accessAllowed = (2>18) ? true : false;
alert(accessAllowed);
It results is an error and none of the JavaScript works.
Being a newbie to JS, I’m unsure if this is a feature of let
. I couldn’t find anything about this elsewhere.
- you're redeclaring by using let the second time – sudo Commented Dec 14, 2017 at 6:37
-
3
You know that
(2>18) ? true : false;
is the same as(2>18)
? – Jonas Wilms Commented Dec 14, 2017 at 6:41
2 Answers
Reset to default 11You cannot use let
to redeclare a variable, whereas you can with var
:
https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Statements/let
Redeclaring the same variable within the same function or block scope raises a SyntaxError.
let
has different (arguably more useful) scoping rules than var
to help prevent many types of bugs caused by var
's quirks which don't exist in other languages but which must be retained in JavaScript for backwards patibility with scripts written decades ago.
Sidenote on let
:
Note that many programming languages have the let
keyword and often use it for declaring variables and constants - however note that each language's use of let
has very different behavior, so do not expect let
in JavaScript to behave like let
in Swift, for example.
- JavaScript:
let
- declares a variable whose scope is limited to the enclosing block, as opposed tovar
which uses either the global scope or the function scope (and understanding howvar
chooses between the two is not easy for beginners to understand). Because redeclaring a variable in the same scope is a meaningless operation that is probably done in-error it will give you a piler error, whereas redeclaring withvar
is valid inside a closure. - Swift:
let
- declares a constant. Note that a "constant" is not just a literal value, but also includes plex objects that are immutable. - Rust:
let
- Introduces a variable binding. Essentially the same thing aslet
in JavaScript except by default values are immutable (like in Swift). Uselet mut
to declare a mutable variable. - C#:
let
is a Linq keyword shorthand forSelect
andSelectMany
. - VBScript, VBA and VB6:
Let
is a keyword that declares a class property setter for a value-type (i.e. not object-types).
Let cannot be re-declared in the same scope ( What you are doing is re-declaring, Rather you should not use the 'let' keyword in the 2nd line and would just reassign ).
本文标签:
版权声明:本文标题:javascript - Redeclaring a variable using `let` results in “Uncaught SyntaxError: redeclaration of let …” - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740163581a2234877.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论