-1

How can I change the value of a constant variable? For example, I have the price on my site:

const PHONE_PRICE = 250;

but now I want to change it in this assignment without making changes elsewhere. Is it possible?

Andrew Korin
  • 294
  • 3
  • 17
  • You can't do that! – Ele Mar 20 '18 at 18:43
  • 2
    There is no such thing as a constant variable. If you mean a Javascript constant, what do you mean by "without making changes elsewhere"? – NetMage Mar 20 '18 at 18:44
  • You cannot change the value of const. Use another const variable for other purpose – Basavaraj Bhusani Mar 20 '18 at 18:44
  • what are you trying to achieve? – messerbill Mar 20 '18 at 18:50
  • @messerbill Asked for the purpose of learning JS. Just wanted to know is there any "tricks" to do that. Like with changing constant arrays and objects with push(), for example... But I know that I actually would change the array/object, not the constant - it is a reference. – Andrew Korin Mar 20 '18 at 19:03
  • you can not change constants. they are - as the name suggests - constant. You can use `let`, which will allow you to change the value of the variable. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let – masterfloda Mar 20 '18 at 19:06
  • Possible duplicate of [Const in javascript? When to use it and is it necessary](https://stackoverflow.com/questions/21237105/const-in-javascript-when-to-use-it-and-is-it-necessary) – jumichot Mar 20 '18 at 20:58

1 Answers1

1

No. The purpose of a constant is precisely that it can't change. If you want it to change, make it a variable:

let PHONE_PRICE = 250;
Oscar Paz
  • 18,084
  • 3
  • 27
  • 42