Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a let block statement and an equivalent with statement?

OBSOLETE

The block version of the let statement was dropped from ES6 before it was finalized, and it has been removed from the browsers that supported it. This question is now only of historic interest.

Is there any difference between using an ECMAScript 6 let block statement and using a with statement with an equivalent object literal?

using let statement

var x = 10;
let (x = x * 10,
     y = x + 5) {
    console.log("x is " + x + ", y is " + y);
}

using with statement

var x = 10;
with ({x: x * 10,
       y: x + 5}) {
    console.log("x is " + x + ", y is " + y);
    // writes "x is 100, y is 15"
}
like image 256
Jeremy Avatar asked Jun 26 '11 14:06

Jeremy


People also ask

What's the difference between LET and VAR?

var and let are both used for variable declaration in javascript but the difference between them is that var is function scoped and let is block scoped. Variable declared by let cannot be redeclared and must be declared before use whereas variables declared with var keyword are hoisted.

What is a block statement?

A block statement is used to group zero or more statements. The block is delimited by a pair of braces ("curly brackets") and contains a list of zero or more statements and declarations.

Should I use let or VAR in JavaScript?

As a general rule, you should always declare variables with const, if you realize that the value of the variable needs to change, go back and change it to let. Use let when you know that the value of a variable will change. Use const for every other variable. Do not use var.

What does with do in JavaScript?

The with statement adds the given object to the head of this scope chain during the evaluation of its statement body. Every unqualified name would first be searched within the object (through a in check) before searching in the upper scope chain.


1 Answers

You can use both with and let statements to achieve the same goal but I see two significant differences here. In the end, the let statement is a new revision of the with statement with the disadvantages of the latter removed.

Performance: In case of the with statement you add an additional JavaScript object to the scope chain. This isn't a small cost, you have to remember that objects have a potentially long prototype chain and so to look up a variable the JavaScript engine first has to search the object and all its prototypes. On the other hand, for a let statement the engine only needs to search at most one additional object. The let statement can indeed be implemented without any overhead at all, since all the variables declared in a let statement are known at compile time and the JavaScript engine can easily optimize the code, e.g. by essentially treating your example like:

var x = 10;
var let1x = x * 10;
var let1y = x + 5;
{
    console.log("x is " + let1x + ", y is " + let1y);
}

Code readability: As already mentioned above, a let statement always makes all declarations visible at compile time, this prevents code like this:

with (foo)
{
    console.log("x is " + x + ", y is " + y);
}

If you look at the code above, what is x and what is y? Are they function variables or properties of the object foo? You cannot tell it without knowing what foo is - and it might be different for different calls of the same function. Which is the main reason the with statement has been deprecated. While you can use it the way you've done in your question (and that is fine), it also allows very questionable and unreadable code constructs. The let statement doesn't - less flexibility is sometimes an advantage.

like image 186
Wladimir Palant Avatar answered Nov 07 '22 03:11

Wladimir Palant