What Is The Difference Between Constants & Variables?

When declaring values in your code, there are two possible ways to do this, one is by creating a variable and the other is by creating a ‘constant’.

Now you might be wondering, bruh what is the difference between the two and when should I use which?

Well, no worries because when you read this blog post till the end, you will discover exactly the difference between the two and when to use which.

Enjoy the read ๐Ÿ™‚


What is a variable?

I like to refer to a variable as a name that refers to a specific value. Let me give you an example.

Take my first name, which is Michiel. Now, how did we identify that this is my first name?
Simple, we call it the “first name” so that when someone asks me, “hey what is your first name?” I respond with, “oh my name is Michiel”.

In code this would look like…

let firstName = "Michiel"

Understand?

Let’s use another example but with that country that I’m from. I’m from the Netherlands so if someone asks me “Hello Michiel, where are you from?” then I would respond with “Oh, I’m from the Netherlands”.

So instead of the name being “first name” it now would be “home country”.

In code this would look like…

let homeCountry = "The Netherlands"

The cool thing about variables is that once we declared & assigned a value to it, we can still change it to whatever we want it to be. So let’s say that I would move to Germany, now, let’s change the value of my homeCountry variable after it’s initial assignment.

let homeCountry = "The Netherlands"

homeCountry = "Germany" // Now, the value will be 'Germany' instead of "The Netherlands".

I hope it makes sense.

Now let’s take a look at…

What is a constant then?

Constants are almost the same as variables, BUT, you cannot change their value after its initial assignment, and, you MUST assign the value immediately after its declaration.

In code, this would look like…

const homeCountry = "The Netherlands"

As you can see, instead of using the ‘let’ keyword (which represents a variable), we now use the ‘const’ keyword in our declaration. So now, if I wanted to still change the value after it’s initial assignment, we would get an error message saying…

// Error: Assignment to constant variable.

Of course, your error message could be different depending on the programming language that you’re using.

But when should I use which?

The answer to that is very simple, do you want to change the value later on in your project, or should be it always the same?

If you answered the first question, you should use a variable, but if you choose the second, then a constant will do the job for you.

Of course, in the end, you can still use variables for both situations but in terms of ‘best practice’, it is recommended that you utilize both.


I hope you learned something from this post and if you did, leave a comment down below and let me know what you though of it :).

Michiel Out!

Oh and btw, and I recently opened my first-ever coaching program to the public and I’m super excited about it. That’s why I’ve decided to allow the first 5 people who join to get FREE coaching, no strings attached.

You check out the program here:
https://michielarkema.com/software-developer-accelerator

And in case you got any questions about it, email me at support@michielarkema.com or contact me on social media.

2 Comments

  1. Mehmet Ali

    I loved the content of this blog post, thank you for teaching us.

    • Thank you so much for the kind comment :), I cannot wait to share more knowledge with you

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.