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 cannot use let to redeclare a variable, whereas you can with var:
https://developer.mozilla.org/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 compatibility with scripts written decades ago.
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.
let - declares a variable whose scope is limited to the enclosing block, as opposed to var which uses either the global scope or the function scope (and understanding how var 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 compiler error, whereas redeclaring with var is valid inside a closure.let - declares a constant. Note that a "constant" is not just a literal value, but also includes complex objects that are immutable.let - Introduces a variable binding. Essentially the same thing as let in JavaScript except by default values are immutable (like in Swift). Use let mut to declare a mutable variable.let is a Linq keyword shorthand for Select and SelectMany.Let is a keyword that declares a class property setter for a value-type (i.e. not object-types).If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With