im new to node and ive been using let and const mostly for declaring variables, but in searching through documentation im reading a lot of references to using var as a variable instead of let, does nodejs use the same variable scoping as vanilla js and the reference documents im reading just outdated?
Yes, both Node and browser run JavaScript in the same fashion, they both adhere to EcmaScript, which specifies the rules of JavaScript.
There are ways to know exactly which ES6 features are supported, but NodeJS has been supporting
let
andconst
since a long time. The only disparity you should probably worry about is ESM vs CommonJS but it doesn’t have anything to do with var/let/const.So I’d encourage you to use
let
andconst
instead ofvar
when writing NodeJS code.You can use either, Node.js supports both let and const since version 6.0.0, see
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let.
I would recommend using block scoped statements:
let
andconst
, since this is more consistent with other languages.