JavaScript const is just a matter of typing 2 more characters and in exchange you make your code more readable (because you communicate "this stuff will never change from here on", which makes understanding the stuff that does mutate easier). There are no far-reaching consequences. Just type "const" everywhere.
I'm not familiar with C++, can you explain more?
First pointers and references. For simplicitly I'm going to ignore references and focus on pointers - the difference is not very interesting in this context. In most languages you probably know, referring to objects is done implicitly: variables that "are" objects are actually references to objects located elsewhere, and variables that are primitive types (numbers, booleans, strings) are just right there. Because of this, in JavaScript you can do this:
var a = {};
var b = a;
b.moo = 6;
a.moo === 6; // true
In here, a and b point to some object that "is" neither a or b - the object itself just floats around in memory and it'll exist until neither a, b, nor anyone else points to it anymore and the GC decides it has to go.In C++, you'll need pointers or references for this, eg.
somestruct a;
somestruct* b = &a; //b now points to a
a.moo = 6;
b->moo == 6; // true
(-> is just C++ shorthand for "follow the pointer and then do a property lookup).Ok now const.
somestruct const a;
a.moo = 6; // error! can't modify a const.
Ok well how about somestruct a;
somestruct* const b = &a;
b->moo = 6;
a.moo === 6; // true
This means the pointer is const. That works because we never change where b is pointing to. So how about: somestruct a;
somestruct const* const b = &a;
b->moo = 6;
a.moo === 6; // true
Oh damn. Crying baby. Const functions will have to wait for some other day.Rust makes it much harder to make these kinds of erros by making variable ownership, reference lifetime, and const-by-default core to the language design. https://doc.rust-lang.org/book/ownership.html ... You do end up spending a lot of time "fighting the compiler," even compared to template-heavy C++ codebases, but the systems you create are free from an entire class of errors.
clang++: error: cannot assign to variable 'b' with const-qualified type 'const somestruct *const'
g++: error: assignment of member ‘somestruct::moo’ in read-only object
Const very rarely saves you from bugs and the bugs that saves you from are very easy to find and fix. On the other hand the time wasted by thinking where whether to write const or let and the time wasted by switching consts for lets (and other way around) outweighs the time saved by potentially preventing these easy to find bugs. To sum it up, if consts does not really provide any extra value, why not make your life easier and just use let everywhere.
It was from very senior c++ programmer, so I am not sure how well that translates to JavaScript.