


JavaScript supports the concept of variables.
A variable can be seen as a container that is used to store data. Most programming languages support variables as well as constants.
Variables are instantiated with an initial value or instantiated without a value. They can then be assigned a value at a later stage or the value stored can be retrieved.
A variable in JavaScript is signified by the var
keyword. The following sample stores the value of 50 inside a variable called 'number' and
then it places the value on the console:
var number = 50; console.log(number);
Constants differ from variables because of the fact that once they have been set they cannot be changed. Constants that exist outwith computer science include Planck's constant (6.626×10-34 j/s) and the speed of light (2.98×108m/s).
Unlike most languages, JavaScript does not feature constants by default. One way of achieving constants in JavaScript is to define a function as follows that would essentially define a constant:
function define_constant(name, value) { Object.defineProperty(window, name.toUpperCase(), {"value" : value, "writable" : false}); } define_constant("Test", 50); console.log(TEST);
![]() | Happy New Year everyone! #2021NewYear 20 days ago |
![]() | Happy New Year everyone! All the best, Jamie Balfour. 20 days ago |
![]() | Retweet @PCMag: Adobe Flash support officially ends today. https://t.co/NNLcFK2yPx ![]() 20 days ago |