What is the difference between 'let' and 'const' ECMAScript 2015 (ES6)?

I'm wondering what is the difference between let and const in ES6. Both of them are block scoped, as the example in the following code:

const PI = 3.14;
console.log(PI);

PI = 3;
console.log(PI);

const PI = 4;
console.log(PI);

var PI = 5;
console.log(PI);

In ES5 the output will be:

3.14
3.14
3.14
3.14

But in ES6 it will be:

3.14
3
4
5

I'm wondering why ES6 allows the change of const value, the question is why should we use 'const' now? we can use 'let' instead?

Note: jsbin can be used for testing, choose JavaScript to run ES5 code and Traceur to run it with ES6 capabilities.

What you're seeing is just an implementation mistake. According to the ES6 spec wiki on const, const is:

A initialize-once, read-only thereafter binding form is useful and has precedent in existing implementations, in the form of const declarations.

It's meant to be read-only, just like it currently is. The ES6 implementation of const in Traceur and Continuum are buggy (they probably just overlooked it)

Here's a Github issue regarding Traceur not implementing const

What's the difference between a pandemic, an epidemic, endemic , What is the difference between isolation and quarantine? Isolation and quarantine are public health practices used to protect the public by preventing exposure to  The key difference between archenteron and blastocoel is that archenteron is the primary gut formed during gastrulation in the developing zygote, which later develops into the digestive tube, while blastocoel is an inner fluid-filled or yolk-filled cavity of the blastula formed during blastulation. Fertilization is the event of sexual

The difference between let and const is that once you bind a value/object to a variable using const, you can't reassign to that variable. In other words Example:

const something = {};
something = 10; // Error.

let somethingElse = {};
somethingElse = 1000; // This is fine.

The question details claim that this is a change from ES5 — this is actually a misunderstanding. Using const in a browser that only supports ECMAScript 5 will always throw an error. The const statement did not exist in ECMAScript 5. The behaviour in is either JS Bin being misleading as to what type of JavaScript is being run, or it’s a browser bug.

In practice, browsers didn't just go from 0% support for ECMAScript 2015 (ECMAScript 6) to 100% in one go — features are added bit-by-bit until the browser is fully compliant. What JS Bin calls ‘JavaScript’ just means whatever ECMAScript features your browser currently supports — it doesn’t mean ‘ES5’ or ‘ES6’ or anything else. Many browsers supported const and let before they fully supported ES6, but some (like Firefox) treated const like let for some time. It is likely that the question asker’s browser supported let and const but did not implement them correctly.

Secondly, tools like Babel and Traceur do not make ES6 ‘run’ in an older browser — they instead turn ES6 code into ES5 that does approximately the same thing. Traceur is likely turning const statements into var statements, but I doubt it is always enforcing that the semantics of a const statement are exactly replicated in ES5. Using JS Bin to run ES6 using Traceur is not going to give exactly the same results as running ES6 in a fully ES6 specification-compliant browser.


It is important to note that const does not make a value or object immutable.

const myArray = [];
myArray.push(1); // Works fine.
myArray[1] = 2; // Also works fine.
console.log(myArray); // [1, 2]
myArray = [1, 2, 3] // This will throw.

Probably the best way to make an object (shallowly) immutable at the moment is to use Object.freeze() on it. However, this only makes the object itself read-only; the values of the object’s properties can still be mutated.

What is the difference between isolation and quarantine?, What are some of these differences, and why do they matter? Contagiousness. One difference between COVID-19 and influenza is that the former  Difference Between Social Business and Social Entrepreneurship (Comments Off on Difference Between Social Business and Social Entrepreneurship) The world is faced with a multitude of pressing issues such as hunger, adverse climate conditions, poor education systems and crime, just to name a few.

What Is the Difference Between Influenza and COVID-19?, What is the difference between Alzheimer's and dementia? Get an overview of each and learn about early symptoms, risk factors, diagnosis and treatment. Difference definition, the state or relation of being different; dissimilarity: There is a great difference between the two. See more.

Dementia vs. Alzheimer's Disease: What Is the Difference?, Isolation separates sick people with a contagious disease from people who are not sick. • Quarantine separates and restricts the movement of people who. The above makes it sound as though a == comparison wouldn't check all the things in the first bullet point, "the same sequence of characters, same length, and same characters in corresponding positions" but in fact it does. As far as I can tell the only real difference when comparing two strings is that with ===, new String ()===new String

[PDF] What is the difference between isolation and quarantine?, Despite the long list of differences, the two masks are equivalent or nearly equivalent on the features that most people care about. N95 vs. KN95  The Percentage Difference Calculator (% difference calculator) will find the percent difference between two positive numbers greater than 0. Percentage difference is usually calculated when you want to know the difference in percentage between two numbers. For this calculator, the order of the numbers does not matter as we are simply dividing

What's The Difference Between N95 and KN95 Masks?, memory, day-to-day life, and communication. Alzheimer's disease is the most common type of dementia. Read the differences and overlap between the two. The names Great Britain and United Kingdom are often used interchangeably. However, they are not actually synonymous. The reason for the two names, and the difference between them, has to do with the expansive history of the British Isles. The British Isles are a group of islands off the northwestern coast of Europe.

What's the Difference Between Dementia and Alzheimer's Disease?, But what are the differences between these two kinds of infectious organisms? Bacteria are tiny microorganisms that are made up  Which vs. That: What’s the Difference in Usage? To understand when to use that or which, it’s important to understand clauses.A defining clause (also called an essential clause or a restrictive clause) gives information essential to the meaning of the sentence.

Bacterial vs. Viral Infections: What's the Difference?, Teeth can be whitened by physical removal of superficial stains using abrasive agents such as toothpaste or prophy paste, or through bleaching processes, which  The distinction between the domains has largely been lost, although there are still non-profit organizations that prefer .org. There are a variety of other domains that were added later to take some off the stress off of the original generic top-level domains (gTLDs), including .biz and .info.

Comments
  • The spec is still under development, let alone implementations. My chrome;32.0.* is silent if you redefine const, but it stays constant. How did you test your code?
  • I am not sure about ESMAScript6 terminology, but in other languages, that would not count as "chang[ing] of const value", it would shadow a constant binding with a different binding.
  • @iccthedral: I JSBin -> choose Javascript to run ECMAScript5 code and Traceur to run it with ECMAScript6 capabilities.
  • Maybe show the code that Traceur genererates - probably it's buggy. Notice that your example would not run at all in ES5 if it was implemented strictly, so you're just experiencing the current browser support of const - some browsers are just treating at as a var for example.
  • So I am still confused here: 1) If const does not make something immutable then why is it called const? 2) If you can reassign the value using let, then how is it different from var? I would appreciate some clarification here. Sorry more I read on this more I am getting confused to identify a clear distinction among these.
  • @rohan-patel 1) const is making the binding (or ‘link’) between the name of a variable and an object constant. If you use const, that variable name will always reference the same object. 2) let is block-scoped, while var is function-scoped. In other words, if you use let inside an if-statement the variable will only exist inside the if-statement, while if you used var, the variable would exist within the body of the function that the if-statement was in.
  • @rohan-patel As for why these two things are useful: if you know a variable name only ever gets assigned to an object once and isn’t changed, it makes your code a lot easier read and think about. As for let, let works the way variables work in most other programming languages, as opposed to var, which doesn't.
  • Very annoying coming from a swift background. Let is supposed to declare non-changing properties.
  • @Jonny I’m sure it is. ES6/JavaScript does have a lot of constraints programming-language-design-wise which Swift doesn’t have, and in my opinion Swift’s ‘var’ vs ‘let’ distinction isn’t as intuitive itself as Rust’s ‘let mutable’ notation.