Language | const | let | var | let mut |
---|---|---|---|---|
C | Immutable variable | Mutable variable | Mutable variable | Not supported |
Java | Immutable variable | Mutable variable | Mutable variable | Not supported |
Python | Immutable variable | Mutable variable | Not supported | Not supported |
JavaScript | Immutable variable | Mutable variable | Mutable variable | Not supported |
PHP | Immutable variable | Mutable variable | Not supported | Not supported |
Ruby | Immutable variable | Mutable variable | Not supported | Not supported |
Swift | Immutable variable | Mutable variable | Not supported | Mutable variable |
Kotlin | Immutable variable | Mutable variable | Not supported | Not supported |
TypeScript | Immutable variable | Mutable variable | Mutable variable | Not supported |
C++ | Immutable variable | Mutable variable | Mutable variable | Mutable variable |
C# | Immutable variable | Mutable variable | Mutable variable | Not supported |
Go | Immutable variable | Mutable variable | Not supported | Not supported |
R | Immutable variable | Mutable variable | Not supported | Not supported |
MATLAB | Immutable variable | Mutable variable | Not supported | Not supported |
Objective-C | Immutable variable | Mutable variable | Not supported | Not supported |
Perl | Immutable variable | Mutable variable | Not supported | Not supported |
Scala | Immutable variable | Mutable variable | Not supported | Mutable variable |
Shell | Immutable variable | Mutable variable | Not supported | Not supported |
SQL | Not supported | Not supported | Not supported | Not supported |
Lua | Immutable variable | Mutable variable | Not supported | Not supported |
Note that this table is a generalization and that some languages may have slightly different semantics for these keywords. Also, some languages may not support all of these keywords or may use alternative syntax for variable declaration.
C
const
: Used to declare a constant variable that cannot be modified.let
: Not supported.var
: Used to declare a variable that can be modified.let mut
: Not supported.
Java
const
: Not supported. (In Java, thefinal
keyword is used to declare constants.)let
: Used to declare a variable that can be modified.var
: Used to declare a variable with inferred type, starting from Java 10.let mut
: Not supported.
Python
const
: Used to declare a constant variable that cannot be modified.let
: Used to declare a variable that can be modified.var
: Not supported.let mut
: Not supported.
JavaScript
const
: Used to declare a variable with a constant value that cannot be reassigned.let
: Used to declare a variable that can be modified.var
: Used to declare a variable that can be modified.let mut
: Not supported.
PHP
const
: Used to declare a constant variable that cannot be modified.let
: Used to declare a variable that can be modified.var
: Not supported.let mut
: Not supported.
Ruby
const
: Used to declare a constant variable that cannot be modified.let
: Used to declare a variable that can be modified.var
: Not supported.let mut
: Not supported.
Swift
const
: Used to declare a constant variable that cannot be modified.let
: Used to declare a variable that can be modified.var
: Not supported.let mut
: Used to declare a mutable variable that can be modified.
Kotlin
const
: Used to declare a constant variable that cannot be modified.let
: Used to declare a variable that can be modified.var
: Not supported.let mut
: Not supported.
TypeScript
const
: Used to declare a variable with a constant value that cannot be reassigned.let
: Used to declare a variable that can be modified.var
: Used to declare a variable that can be modified.let mut
: Not supported.
C++
const
: Used to declare a constant variable that cannot be modified.let
: Not supported.var
: Used to declare a variable that can be modified.let mut
: Used to declare a mutable variable that can be modified.
C#
const
: Used to declare a constant variable that cannot be modified.let
: Not supported.var
: Used to declare a variable that can be modified.let mut
: Not supported.
Go
const
: Used to declare a constant variable that cannot be modified.let
: Used to declare a variable that can be modified.var
: Not supported.let mut
: Not supported.
R
const
: Used to declare a constant variable that cannot be modified.let
: Used to declare a variable that can be modified.var
: Not supported.- `let
MATLAB
const
: Not supported. (In MATLAB, theconst
keyword is used to declare classes with constant properties.)let
: Used to declare a variable that can be modified.var
: Not supported.let mut
: Not supported.
Perl
const
: Not supported.let
: Used to declare a variable that can be modified.var
: Not supported.let mut
: Not supported.
Lua
const
: Not supported.let
: Used to declare a variable that can be modified.var
: Not supported.let mut
: Not supported.
Objective-C
const
: Used to declare a constant variable that cannot be modified.let
: Not supported.var
: Used to declare a variable that can be modified.let mut
: Not supported.
PowerShell
const
: Used to declare a constant variable that cannot be modified.let
: Not supported.var
: Used to declare a variable that can be modified.let mut
: Not supported.
Rust
const
: Used to declare a compile-time constant.let
: Used to declare a variable binding that is immutable by default.var
: Not supported.let mut
: Used to declare a mutable variable binding.
SQL
const
: Not supported.let
: Not supported.var
: Not supported.let mut
: Not supported.
Visual Basic
const
: Not supported.let
: Not supported.var
: Used to declare a variable that can be modified.let mut
: Not supported.
Conclusion
As you can see, the semantics of const
, let
, var
, and let mut
can vary widely across programming languages. While some languages may not support certain keywords at all, others may have additional keywords for variable declaration. It’s important to understand the nuances of each language’s variable declaration syntax to avoid errors and make the most of the language’s features.