DEV Community

Discussion on: ES6 and Beyond > Part 1

Collapse
 
jamesthomson profile image
James Thomson

Good article and good uses cases for pad.

May I suggest a clearer example for let. While your example is correct, I think this really drives block scope context home:

if (true) {
  var foo = 'foo';
  let bar = 'bar';
}

console.log(foo) // foo
console.log(bar) // Uncaught ReferenceError: bar is not defined

Also, regarding const (which is also blocked scoped),

we can not re-assign a value to it

It should be noted that this only applies to the value assignment. Nested values are still mutable:

const foo = {
  value: 'foo'
}

foo.value = 'bar';

console.log(foo.value) // bar

however, the value of a const cannot be changed:

const foo = {
  value: 'foo'
}

foo = {
  value: 'bar'
};

// Uncaught TypeError: Assignment to constant variable.
Collapse
 
elanandkumar profile image
Anand Kumar

Thank you.
I do agree with your examples above. Thanks for providing the same.
I did want to cover mutation and more in depth but on the other hand, I just wanted to keep things simple.

Collapse
 
jamesthomson profile image
James Thomson

Fair enough. It can be hard to keep things simple yet cover all the foundations :)

Thread Thread
 
elanandkumar profile image
Anand Kumar

Thank you.