Constants in JavaScript

In addition to variables in JavaScript, you can also create constants. They can only be written to once, and then their values cannot be changed. Constants are declared using the operator const.

Typically, constants are used for values that should not be accidentally changed in code. For example, let's create the constant that will store the value of pi:

const pi = 3.14;

Constants are usually written in capital letters so that they can be distinguished from regular variables:

const PI = 3.14;

Let's try to change the value of our constant:

const PI = 3.14; PI = 3; // throws an error

Create the constant PI and use it to calculate the circumference with a given radius.

enru