Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Creating Unchanging Variables With const

Jose Balaguer
Jose Balaguer
17,473 Points

Why do you say constants are variables?

Having a background in science I know that constants are not variables by definition. Those are opposite concepts. So I'm trying to figure out why you keep saying constants are a type of variables. I don't know if it's some JS trick but I think the terms are misused here.

Steven Parker
Steven Parker
229,644 Points

Don't confuse traditional "science" with "computer science".

They do not always use the same words to mean the same things. When someone in computer science says "constants are types of variables", what they mean is "constants are specific types of values which are stored in computer memory and have symbolic identifiers".

See my answer below for more details.

7 Answers

Steven Parker
Steven Parker
229,644 Points

:point_right: Variables in programming may not directly correspond to the concept of variables in mathematics.

That's a direct quote from the wikipedia page for "Variable (computer science)". The page also provides the following as the basic definition:

In computer programming, a variable or scalar is a storage location paired with an associated symbolic name (an identifier), which contains some known or unknown quantity of information referred to as a value.

Clearly, as defined here, the term could apply to values that cannot be changed. While I personally agree that using this term to refer to stored constants is unnecessarily contradictory to its mathematical usage, it is both correct and customary within the realm of computer science to do so.

I would, however, be inclined to consider treating these terms more consistently with their mathematical counterpoints to be a best practice; and fully support the promotion of it. I think that continuing to be tolerant of the traditional use of these terms in programming while simultaneously exemplifying and promoting change is the best of all possible approaches.

Constants are variables are similar in many ways, but also different in many ways.

Think of a variable as a box. You can put values in it, and you can replace those values by re-assigning them.

With a variable, you have free access to everything in it and can see what's in the box, or replace/edit what's inside the box.

However, constants are a little different.

You can think of a constant as a locked glass box. You can see what's inside (the sides are glass), but since it's locked, you can't change/edit what's inside.

You might think "Hmm... You con't change constants?! Seems like a useless thing", but, in some situations, it can be useful.

For instance, pretend we are making a graphical game, and we are storing the width and height of the canvas (screen). You could use a variable, but since we probably aren't gonna twiddle around with the values of the width and height, so, to save up memory, you can use a CONSTANT instead.

Constants are values that stay constant. Lol. It's true, though XD

Looking up "What's the difference between variables and constants in JavaScript" and you should get some sweet results, too :)

Good luck! ~Alex

Jose Balaguer
Jose Balaguer
17,473 Points

I understant perfectly how both work. Still constants are not variables. Although you can assign values to both they are diferent things. Those are basic mathematical concepts that nobody seems to know here.

Mathematics aren't always like computer science.

Read Steven Parker's post and please un-best answer your answer :)

Your post is incorrect

You can more easily think of it as a variable because you can change the value. Just not inside the program. So if you ever decide to update the program and change something because lets say the tax rate changed you can change it.

This is common in programming. They are referring more to the identifier as a variable, but the value is a constant.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const

EDIT:

if you define a variable with const

This quote can be found in the first sentence after the headline 'Compatibility notes'.

Jose Balaguer
Jose Balaguer
17,473 Points

Exactly, in the docs says clearly that consts ARE NOT variables. You confirm my point. Teachers need to see it too :)

Michal Janek
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Michal Janek
Front End Web Development Techdegree Graduate 30,654 Points

They are variables just the word variables in programming means a different thing than what you want it to refer to. So un-best your answer please finally so we can get over it.

I think its a good point to highlight however its not like the instructors of this course were the originators of the term for the entire field or JS (to my knowledge).

In any industry, it is good practice to align yourself to the common meaning of the terminology being used. For example, "CSR" is widely known throughout various industries to stand for the Customer Service Representative. In Telecommunications however CSR actually stands for a Customer Service Record, which is a detailed inventory report of the customers services. So when providing information to that group, I need to be aware of that. (Though there are CSRs that would gladly produce a CSR for you if you requested one ;)).

Generally speaking, know your arena and the intended audience for the information being provided/received.

Jose Balaguer
Jose Balaguer
17,473 Points

Hey you guys. MDN docs: " Statements and declarations (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements): . var declares a variable . let declares a local variable . const declares a constant. Try to assume that freaking concept of a constant not being a variable. Correct and grow and your life will be awesome :)

I know you are upset about this, but if you want to get along with other programmers, this is the lingo. I do have a degree and have taken a lot of math--almost enough to have a degree in math alone if I wanted.

You will see it used everywhere in programming, just look at this Java documentation

If your variable stores a constant value

or from Microsoft

Both Java and C# provide the ability to declare a variable whose value is specified at compile time and cannot be changed at runtime. Java uses the final field modifier to declare such a variable, while C# uses the const keyword

You are WRONG. Read Steven Parker's post.

Martyn Masson
Martyn Masson
3,266 Points

I'm not a programmer or a genius mathematician but in my view here, the const is technically a variable in the sense that the value can be changed by the programmer but obviously at runtime it's value cannot change.

It's kind of like saying pi is a constant value but it's not because before running pi I can change it from 3.14 to 3.1415926. So technically its a constant number but before I've ran my program I changed the value.

This little explanation is as clear as mud :-)