3

I always thought that const variables in JavaScript would be constants... like "variables that cannot change".
So I always assumed the benefit of using const over let/var would be something like decreased resources usage.
But after a quick test it looked like a const pretty much acts like a let-variable. It's block scoped and can be modified... at least in my browser (Firefox 47.0).

Here's what I did:

const FOO = [0,1,2] FOO[0] = 11 console.log(FOO) // > [11, 1, 2] 

And that leads to my question: What is the point of using const? I mean the word is longer, lot's of people don't know of const and it straight up seems to act like a normal variable. So why would I give the illusion that it is not? Why don't we just keep writing constants as let/var with the name in all-caps?

8
  • 9
    You are confusing mutating the value with assigning a new value of the variable. You cannot assign a new value to FOO (i.e. FOO = 23) because FOO is const. But because objects are mutable in JavaScript, you can still mutate the object itself. Related questions: stackoverflow.com/q/31205975/218196, stackoverflow.com/q/22308071/218196, stackoverflow.com/q/26015747/218196 Commented Aug 3, 2016 at 17:22
  • 2
    You must be looking for Object.freeze() or seal. Memory locations cannot be replaced when using const, i.e It will throw error at that time. Commented Aug 3, 2016 at 17:23
  • What is constant is the objet reference, not the properties of that object. Commented Aug 3, 2016 at 17:26
  • Let's say you're looking at and maintaining hundred thousand lines of code. Will a final variable throughout it (especially in a team of developers) be more apparent as a const variable or a var variable? It provides the ability to make code cleaner. I would think it does nothing but provide a benefit to the language as a whole. Commented Aug 3, 2016 at 17:30
  • But hasn't one of the points of JavaScript been its lack of data types? I mean what is next? String and Bool? You could certainly argue that by that it would become more apparent what a variable does. But in my opinion a complete lack of data types is a huge advantage. Commented Aug 3, 2016 at 17:37

3 Answers 3

3

To put it simply, the value of the object/array doesn't change, you are only mutating the properties of the object. To demonstrate:

const FOO = {}; FOO.something = 'foo'; // [legal] mutating the object, but still the same object FOO = {}; // [illegal] attempt to assign new object 

Working with objects is a little less straightforward when learning about const versus other primitive types:

const FOO = 1; FOO = 2; // [illegal] attempt to assign new value FOO = []; // [illegal] attempt to assign new value 

To answer the question, const isn't needed. It's a luxury of programming that prevents programmers from shooting themselves in the foot.

It's syntactic sugar that helps to secure values and communicate ideas to maintainers, but JavaScript developers have been able to program just fine without it for years. It only helps the language become more refined by helping to detect and prevent errors earlier and enforce some consistency.

As applications/modules become more complex, the need for interpreter/transpiler assistance becomes that much more important and const can be a huge benefit there.


Also of note, there are other added benefits. JavaScript engines are more efficient when working with constants. While the performance boost may be marginal, some very intensive applications must free as much resources as they can.

It's easier to think of setting constants for popular variables:

const PI = 3.1415926535; const E = 2.71828; const GOLDEN_RATIO = 1.61803398874; const GRAVITY = 9.807; 

By convention, I use all caps to distinguish globals and constants.

Sign up to request clarification or add additional context in comments.

Comments

0

The point of using const is creating immutable bindings instead of mutable ones.

let foo = 1; foo = 2; // 2 foo; // 2 
const bar = 1; bar = 2; // TypeError: invalid assignment to const `bar' foo; // 1 

So you can't mutate the value stored in that variable.

However, you can mutate the data associated with the value in that variable.

const obj = {foo: 'bar'}; obj = [1,2,3]; // TypeError: invalid assignment to const `bar' obj; // {foo: 'bar'} obj.foo = 'baZ'; // 'baZ' obj.foo; // 'baZ' 

If you don't want to alter some properties of an object, you can define them as non-writable or non-configurable. If you want to prevent the addition of new properties, you can seal or freeze. But these concepts are completely different than constant variables.

Comments

-1

suppose you have some hardcoded things to compare like

if(somevariable==""){ do something } 

Another approch is

const EMPTY_STRING = "" if(somevariable==EMPTY_STRING){ do something } 

sometime what happen when you develop thing you do compare things like if(somevariable == " "){}

you miss space there, we can avoid such mistakes.

use 2:

We can have hardcoded thing in const variable which will be used in so many places in js file. so you can hardcod it once only

2 Comments

I don't really see why const would be necessary for that tbh.
suppose you have to compare one varible with value 5 in 5 times in one js file. How will you do. if(someVariable === 5). isnt it?? now I can store it in var also. so why const. It do not allow you to change it mistakenly also because it is constant and supposed to be contant

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.