logo

Programming Languages - Constants and Variables

By Language

C++

Check const vs constexpr

Kotlin

  • const is for compile-time constants (e.g. creating constant strings similar to final static in Java), this is similar to constexpr in C++;
  • val is for read-only variables (similar to final in Java), and can be assigned at runtime; similar to const in C++
  • var is for mutable variables.

JavaScript

JavaScript also has const but it simply means "cannot be reassigned", it may NOT be a constant and it may NOT be immutable

const a = [1, 2];
a.push(3); // this is OK, a will be [1, 2, 3]

To achieve immutablility, use Object.freeze:

Object.freeze(a);
a.push(4);
// Uncaught TypeError: Cannot add property 4, object is not extensible

JavaScript:

const, var, let

Go

const, var