Hi James,
I’d love to go a little deeper into this with you, because there are some nit-picky nuance items about this idea.
The keyword ‘const’ indeed refers to a constant. It is, as you have guessed, used in a condition where we don’t want the variable to change.
Where to use a const? That can be difficult to conceptualize when we’re trying to think of a thing that doesn’t need to change: bullets count down and reload, score points add up, hit points go down.
Where would we ever need a ‘const’?
Well, in game development, it might be difficult to find a place. We declare variables all the time because the gaming environment is so dynamic, both in-game and outside. Expansion packs will add weapons and spells and interfaces, etc…everything is constantly changing.
// BeginRant();
Also, now for the nit-picking…If it can’t change, it shouldn’t be called a variable. Period. By definition, variables are expected to ‘vary’. That’s why they’re called ‘variables’…because they are ‘able to vary’. It bugs me when people say ‘a variable that doesn’t change’ because it’s bad grammar…
…It’s like when the news reads ‘armed gunman’ but they know the gunman was already armed, because he’s a friggin’ gunman, der! Each and every gunman happens to be a person armed with a gun. That’s what makes them gunmen. Armed is implied in the title of ‘Gunman’…but that’s me being a nit-picky word-nerd.
// EndRant();
So yeah, a constant is a variable that doesn’t change.
In web development, and I use this in JavaScript a lot, we declare anything as a ‘const’ until we decide that it needs to change. Once we notice a need for something to change, we will change the declaration from ‘const’ to ‘let’ (JS, remember). We can use ‘var’ in JS, but it’s an older practice.
Here’s the fun part: JS is not as strongly typed as C#, an an array in JS can act like a list (change size, size does not have to be declared) and hold any type, and MULTIPLE types of data! It’s nuts!
So, in JS, one could declare a:
const array[userName, pin, dob, occupation, vehicleType, age = currentYear - dob, cityOfResidence];
In this array, we would have:
[
userName: String
pin: int
dob: int
occupation: string
vehicleType: object
age: an expression returning a value to the variable ‘age’
cityOfRedidence: string
]
…and keep adding stuff to it and removing stuff from it, even though it is a const. This is acceptable behavior for an array in JS. Way different from strongly typed C#, and of course, for it’s own reasons.
It’s really cool! A little confusing, but definitely cool!
Also, I’m not a very experienced programmer either. I’ve been studying for a couple of years, but I haven’t earned my entry level job yet. I still have a lot to learn.
Keep at it, and keep making games!
I hope this is helpful!