DEV Community

Discussion on: let var be const

Collapse
 
mortoray profile image
edA‑qa mort‑ora‑y

I'd like to note that C/C++ have both concepts, but usually refer to value constness.

T const * ptr = expr;

The T value is const here, not the pointer. This is usually what we want. Compare to:

T * const ptr = expr;

The pointer is constant but the value is not. This is what const appears to mean in JavaScript, which is misleading based on it's syntax.

You can have both at the same time in C/C++:

T const * const ptr = expr;

Read the type form right-to-left to understand where const applies (the syntax rules are a bit ugly for this in C).