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 andEDIT:
This question was asked quite a while ago. As of now, late 2015, the answer is "Yes, yes it does". Harmony features were included by default in io.js 3.3, and have been recently brought back to node.js with the 4.x release.
let allows you to declare variables that are limited to the scope of a block statement, or expression on which it is used, unlike the var keyword, which declares a variable globally, or locally to an entire function regardless of block scope.
The let keyword was introduced in ES6 (2015). Variables defined with let cannot be Redeclared. Variables defined with let must be Declared before use.
Use let when you know that the value of a variable will change. Use const for every other variable. Do not use var.
js receives a CPU bound task: Whenever a heavy request comes to the event loop, Node. js would set all the CPU available to process it first, and then answer other requests queued. That results in slow processing and overall delay in the event loop, which is why Node. js is not recommended for heavy computation.
Yes, 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With