javascript - Why does Node.js commonly use 'var' instead of 'let'? - Stack Overflow

Basically every tutorial I’ve watched on Node.js, and even the express generator has all variables decl

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.

Share Improve this question edited Sep 16, 2019 at 6:17 insertusernamehere 23.6k10 gold badges91 silver badges128 bronze badges asked Sep 16, 2019 at 6:00 BrandonBrandon 3774 silver badges12 bronze badges 5
  • 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
Add a ment  | 

1 Answer 1

Reset to default 6

It 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信