Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does node.js support the 'let' statement?

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

  1. has let and and
  2. has a REPL, as node does? Rhino?

EDIT:

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.

like image 278
ben author Avatar asked Jul 01 '12 16:07

ben author


People also ask

What is let in node JS?

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.

Can we Redeclare let?

The let keyword was introduced in ES6 (2015). Variables defined with let cannot be Redeclared. Variables defined with let must be Declared before use.

Should I use let or VAR?

Use let when you know that the value of a variable will change. Use const for every other variable. Do not use var.

For which node JS is not recommended?

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.


1 Answers

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.

like image 117
Timothy Strimple Avatar answered Sep 23 '22 08:09

Timothy Strimple