"Let
allows you to declare variables that are limited in scope to the block, statement, or expression on which it is used. This is unlike the var
keyword, which defines a variable globally, or locally to an entire function regardless of block scope." - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let
Which one should be used in modern javascript code? let
or var
?
Edit:
I checked the Google JavaScript Style Guide and it states "Declarations with var: Always" (https://google.github.io/styleguide/javascriptguide.xml)
But in the tutorial at javascript.info (not sure if this is an official JavaScript website or not) it says to use let
(https://javascript.info/variables)
I'm assuming I should stick to what Google recommends, but am still open to answers.
Second Edit: The above is not the most recent Google JavaScript Style Guide. In that one it states: "Declare all local variables with either const
or let
. Use const
by default, unless a variable needs to be reassigned. The var
keyword must not be used".(https://google.github.io/styleguide/jsguide.html#features-local-variable-declarations)
No comments:
Post a Comment