Basically every tutorial I’ve watched on Node.js, and even the express generator has all variables declared using var
instead of let
? From what I’ve learned in Javascript.info let
should be the standard unless for very niche cases.
Basically every tutorial I’ve watched on Node.js, and even the express generator has all variables declared using var
instead of let
? From what I’ve learned in Javascript.info let
should be the standard unless for very niche cases.
-
const
should be preferred, except in odd cases where a variable needs to be reassigned.let
isn't so good - in good code, it's a warning to readers of the code that it's a variable that's going to be reassigned later (and reassignment should be avoided when not necessary, since it makes the code harder to understand at a glance) – CertainPerformance Commented Sep 16, 2019 at 6:01 - Instead of assuming there is some sort of standard without really knowing the reason or the effect, it would make more sense to go and research what the different keywords do, and then, each time you declare a variable, use the most appropriate one for the situation – ADyson Commented Sep 16, 2019 at 6:04
- 2 Not related to Node.js, but sometimes documentation is just old and changing small details like that (which don't have too much influence in the documentation itself) is just a lot of work for little improvement. – Gerardo Furtado Commented Sep 16, 2019 at 6:07
-
1
There is no good reason that any body of recently written code would mostly or pletely use
var
unless they were trying to maintain patibility with old JS engines. Could just be old code. – jfriend00 Commented Sep 16, 2019 at 6:34 - 1 The reason is that there a lot of old tutorials out there. Regarding the Express generator, they want to support the Node Version that Express 4 supports. github./expressjs/generator/issues/167 – madflow Commented Sep 16, 2019 at 7:00
1 Answer
Reset to default 6It is because the Node.JS code is written in ES5 which does not support the use of let and const. If you still wish to write your code in ES6, you need to use something called Babel which will help Node.JS to convert the ES6 code into ES5. But you can definitely use let and const in Node. I hope the answer helps.
Please refer to the below link for an in-depth explanation. https://dev.to/dhruv/writing-es6-in-your-nodejs-applications-33jk
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745299064a4621323.html
评论列表(0条)