Does node.js support a let statement something like what's described on MDN??
var x = 8,
y = 12;
let ( x = 5, y = 10) {
return x + y;
} //15
If not, is there a way to duplicate the functionality with a self-executing anonymous function or something?
And/or is there another js environment that
let
and andYes, you can use let within node.js, however you have to run node using the optional --harmony flag. Try the following test.js:
"use strict"
var x = 8,
y = 12;
{ let x = 5, y = 10; console.log(x + y); }
console.log(x + y);
And then run the file node --harmony test.js
which results in:
15
20
I would not recommend using this in an important production application, but the functionality is available now.
node --use_strict --harmony_scoping
I don’t think Node supports let
, but you can do this:
var a = 5;
(function () {
var a = 6;
console.log(a); // => 6
})();
console.log(a); // => 5