# What is a constant

Constants in programming are similar to variables, with the (huge) difference that once a value is assigned, it cannot be modified.

During the execution of our program, there are certain values that will not change. The most typical example is the value of PI, which will not change throughout the execution of your program. But there are many more.

``````// Constant declarations
const PI = 3.14159
const DAYS_IN_WEEK = 7
const SECONDS_IN_MINUTE = 60
const MONTHS_IN_YEAR = 12
const SPEED_OF_LIGHT = 299792458  // in meters per second``````

These are “quite absolute” constants. They are like this because they are part of our reality. But they are not the only ones we will have.

For example, imagine you are making a program that manages vehicles. For you, in the model you have created, a vehicle can only have a maximum of four wheels. So you would have

``const MAX_WHEELS = 4``

What would happen the day you have to manage a truck with 8 wheels? Well, basically, you have a problem and you will have to review your code 😞.

But what is important right now is to clearly see that what is constant, and what is not constant, depends on your program and the solution you have planned to give it.

## How is a constant defined?

Most languages have a reserved keyword to indicate defining a constant. In most languages, the word is `const` (although not in all).

Since a constant cannot be reassigned another value, in general, the assignment must be done at the time of declaration.

So, for example, in C++, Java or C# (strictly typed) a constant is declared like this

``const int MAX_WHEELS = 4``

While in JavaScript (dynamically typed) it would not be necessary to indicate the type, and it would look like this.

``const MAX_WHEELS = 4``

However, not all languages have constants. For example, Python does not have the concept of a constant. Therefore, normal variables must be used.

## What are constants for?

We have seen that constants behave very similarly to a variable (in the sense that they are containers where we can store data).

So, what is the usefulness of not being able to reassign the content? Well, they have a couple of advantages.

The main one is that they increase the security and cleanliness of the code. They prevent a value from being accidentally changed. If I try to reassign a constant, the compiler or interpreter will tell me “hey, buddy!, you can’t do that!“.

On the other hand, it improves the readability of the code. When you mark a variable as constant, you are telling the next programmer not to worry too much about that value, as it will not change.

Finally, it can also improve the performance of the program, as compilers can optimize the code that uses constants. For example, they can decide to store them in another memory area, or even completely replace it with its value.

The latter depends on the compiler we use. Most modern compilers are capable of identifying constants, even if we do not mark them as such. But, in general, if it has any effect on performance, it will be a small improvement.