Are constants variables?

Are constants a type of variable in computer programming?

  • Yeah, of course

    Votes: 5 50.0%
  • No, that's counter-intuitive

    Votes: 5 50.0%

  • Total voters
    10

kognise

Member
Local time
18:57
Joined
Oct 5, 2019
Messages
44
Pronouns
He/him/his

In computer programming, do constants count as a type of variable? Or is that counter-intuitive.

I've always thought of constants as a type of variable but it doesn't really make sense and I'm not sure about what other people in the industry think. It feels weird saying "define a constant variable" 🤣

Disclaimer: not sure that this is the best place to put this but there doesn't seem to be a general programming forum/subforum.

 

Gummibeer

Astroneer
Moderator
Local time
00:57
Joined
Oct 5, 2019
Messages
1,177
Pronouns
he/him

If they are real constants they aren't a sub type of variables. But there are languages, like JavaScript, that only don't allow an assignment but manipulation of a "constant" array/object.

In computer programming, a constant is a value that cannot be altered by the program during normal execution, i.e., the value is constant.[a] When associated with an identifier, a constant is said to be "named," although the terms "constant" and "named constant" are often used interchangeably. This is contrasted with a variable, which is an identifier with a value that can be changed during normal execution, i.e., the value is variable.

 

m1guelpf

18-year-old maker
Local time
01:57
Joined
Oct 5, 2019
Messages
57
Pronouns
he, him

Variable: Something that can change (or vary)
Constant: Something that is constant (aka can't change)

Seems like a no to me 😃

 

kognise

Member
Local time
18:57
Joined
Oct 5, 2019
Messages
44
Pronouns
He/him/his

Variable: Something that can change (or vary)
Constant: Something that is constant (aka can't change)

Seems like a no to me 😃


I agree! But look at this nonsense:

"constant variable"

 

Gummibeer

Astroneer
Moderator
Local time
00:57
Joined
Oct 5, 2019
Messages
1,177
Pronouns
he/him

like JavaScript, that only don't allow an assignment but manipulation of a "constant" array/object.
I've already said this.^^ JS doesn't implement constants, it only implemented a third keyword to declare a variable with limitations. The const keyword is the most misleading thing in JS.

 

Ilgıt

Member
Local time
00:57
Joined
Oct 6, 2019
Messages
5

@kognise one of the hardest thing in programming is "naming" things.

Imagine it like a "tutorial". When you prepare a tutorial, you need to consider that viewer might have "no experience" on the subject.

It might be the same thing here; being more "beginner friendly"

 
Last edited:

kognise

Member
Local time
18:57
Joined
Oct 5, 2019
Messages
44
Pronouns
He/him/his

like JavaScript, that only don't allow an assignment but manipulation of a "constant" array/object.

Although arrays/objects seem modifiable even stored as a "constant," it's technically still a constant. It's storing a constant pointer to the array/object internally that cannot be modified. Thus, you cannot assign it a new value. But if you modify the array/object the pointer is staying the same.

Sorry if I'm being dumb, my brain is only 50% functional right now 🙃

 

joshkmartinez

New member
Local time
15:57
Joined
Oct 5, 2019
Messages
1

I think constants are variables. While it is counter-intuitive, as their value cannot "vary", my definition of a variable does not require it to be changeable.
According to Wikipedia (I know not the best source), In computer programming, a variable or scalar is a storage location (identified by a memory address) paired with an associated symbolic name, which contains some known or unknown quantity of information referred to as a value.
Nowhere in that definition does it say that variables have to be changeable. A variable is simply a pointer to a location in memory.

 

Gummibeer

Astroneer
Moderator
Local time
00:57
Joined
Oct 5, 2019
Messages
1,177
Pronouns
he/him

@joshkmartinez to stay with Wikipedia the definition of constant clearly states that variables are the opposite of a constant.


So I would say it also depends on your definition of "variable".^^ For me as a high-level developer it's not important if they are ram pointer or save into a filepath or whatever. I only care about the effect in my runtime. And in this case constants and variables are the absolute opposite of each other.

 

eyelidlessness

New member
Local time
15:57
Joined
Oct 14, 2019
Messages
1

A constant is an unchangeable value assigned to a name. (Some languages like JavaScript restrict "unchangeable" to prevent only reassignment but still allow mutation.) But what that value is can vary. A constant within the lexical scope of a function or block can be assigned a value dependent on the outer scope's state. A constant is a variable that can only be defined (or assigned) once.

 

yangshun

New member
Local time
15:57
Joined
Oct 14, 2019
Messages
3

I wondered that question myself too. I interpret it this way - a variable is a container that can store varying values. A constant is a container that stores a value which doesn't change.

 
Top