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 trialjlampstack
23,932 Pointsprice as a const?
Andrew mentions that price is a good example of a variable that should be const, but prices change all the time. Please elaborate why so I can better understand. Thanks!
6 Answers
Joseph Wasden
20,407 PointsI think this is mostly just an instructional design choice Andrew made to illustrate how one might use Const. Commerce is complicated; while in the aggregate, prices shift according with market pressures, it also isn't uncommon to find flat-rate service fees, etc. Perhaps it wasn't the best example.
One way of looking at const is, will this value need to change during the life of the program after it is assigned? Perhaps with each run of the program, we will calculate the necessary shifts in the market, and then assign it to a const variable, since once the price is calculated and assigned, we don't expect it to change.
However, do you feel you have a firm grasp of what const is? It seems you do, if you are asking questions like these. If there is still some confusion, lets hash it out!
Masha Blair
13,005 PointsI agree with Joseph's explanation. And I would add that using const for price and for money values in general is especially useful so that program doesn't change the value by mistake creating a chaos in a database. If the price is changed because of a discount , for example, that would be assigned to a new variable leaving the initial price unchanged.
jlampstack
23,932 PointsThanks Joseph Wasden for clearing this up for me. Just one more question if I may. If we have a flat rate product, but every year there is a price increase, is it best practice to use let or const?
Joseph Wasden
20,407 PointsI would probably use const to represent the price in my program, and assign the const its value using some kind database query result. If the price changed, I would update the database, so the program could still work just fine regardless of price changes.
jlampstack
23,932 PointsOK Great! Thanks again!
Joseph Wasden
20,407 PointsYep! Good luck, and see you in the forums!
Libor Gess
6,880 PointsGood evening,
this is quite confusing for me because const
or constant is something or someone that does not change as constant. The const
value you cannot change it because TypeError: Assignment to constant variable
.
Joseph Wasden
on Oct 31, 2017
I would probably use const
to represent the price in my program, and assign the const
its value using some kind database query result. If the price changed, I would update the database, so the program could still work just fine regardless of price changes
Joseph Wasden
20,407 PointsI could have worded it better. Let's say we have a const named PRICE. In my code, I assign it the value in a database for some item. So, my program would check the database, get the price from that database, then assign it to the const PRICE. My program won't allow my variable PRICE to change during the life of the program, which is presumably what I want. However, if someone updates the database entry where I am getting price from, the next time my program runs, it will assign that newly updated value from the database to my const PRICE.
Libor Gess
6,880 PointsThank you for answer. Now I understand what did you meant. Thank you.