+ 1
What is the use of constants?
why would I use a constant instead of a Variable? I understand that a constant cant be changed but I can simply use a variable and not change it
4 Respuestas
+ 5
Variables can be undefined, constants not.
It's more a caution meassure. In bigs proyects, where you have thousands variables, you might change an unwated var. But you can't change the CONST value, so its safer, and don't let you commit clunky errors.
+ 3
Let's suppose that you want to have a number of PI. You want to use constant value here, as PI number is unique and you do not want to ever alter it in future. Safety purpose mainly.
+ 1
Didn't allowed to change. like some date of app.It's safety!
0
Using constants is more a way of defensive programming, to protect yourself from yourself, from accidentally changing the value somewhere in the code when you're coding at 2 a.m. or before having drunk your coffee.