The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

555405 (1) [Avatar] Offline
#1
Looking at the D.20 example, you see this code:

for (let i = 0; i < 3; i++)
  console.log(i);
  console.log(i * 5);


The book states that results should be like this:
// Output in the console
// 0
// 1
// 2
// 15        
  

When running this, I get an error that I is not defined. If you change "let" to "var," the output is as expected. Is this due to the lexical scope being only the first statement, which is implied by leaving out the curly braces?

My initial instinct is that this is an error in the example in the book, but I'm trying to make sure I'm understanding correctly.
Clive.Harber (13) [Avatar] Offline
#2
Hi

Thanks for pointing this out.

There is indeed an error in the manuscript here and yes, it is down to the lexical scoping of the let binding - the binding is correct for the example so the expected response should be:

// Output in the console
// 0
// 1
// 2
// Uncaught ReferenceError: i is not defined


It is being corrected in the manuscript.

Once again, thanks for finding this and thanks for reading the book

Clive