JavaScript Getting Started With ES2015 Defining Variables With let and const Creating Unchanging Variables With const

jlampstack
jlampstack
17,752 Points

price 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
Joseph Wasden
20,373 Points

I 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
Masha Blair
12,997 Points

I 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
jlampstack
17,752 Points

Thanks 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
Joseph Wasden
20,373 Points

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.

jlampstack
jlampstack
17,752 Points

OK Great! Thanks again!

Joseph Wasden
Joseph Wasden
20,373 Points

Yep! Good luck, and see you in the forums!

Libor Gess
Libor Gess
4,891 Points

Good 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
Joseph Wasden
20,373 Points

I 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
Libor Gess
4,891 Points

Thank you for answer. Now I understand what did you meant. Thank you.