Constants in JavaScript
Go supports constants of character, string, boolean, and numeric values.
const
declares a constant value:
const s = "constant";
console.log(s);
A const
statement can appear anywhere a var
statement can.
const n = 500000000;
Constant expressions perform arithmetic with arbitrary precision.
const d = 3e20 / n;
console.log(d);
A numeric constant has no type until it’s given one, such as by an explicit conversion.
console.log(BigInt(d));
A number can be given a type by using it in a context that requires one, such as a variable assignment or function call. For example, here Math.sin
expects a number
.
console.log(Math.sin(n));
To run the program, save the code in a file (e.g., constants.js
) and use node
to execute it:
$ node constants.js
constant
6e+11
600000000000n
-0.28470407323754404
Now that we can run and build basic JavaScript programs, let’s learn more about the language.