Javascript: Working with Constants

Variables in Javascript which can take variable values during program execution. But if you want to work with constants like PI, you can make use of const.

The syntax is

const name [= value];

The best example for a const is PI (used in various mathematical formulas)

const pi = 3.14;

There are some more things that one can understand from the above syntax. The following line is completely fine

const interest;

If you have followed the post on assignment of variables, you will know that interest has the value undefined.

Now let’s try to declare interest again

const interest;
const interest = 3.0;

You will get the following error

TypeError: redeclaration of const interest

But the following statements are totally fine

var interest;
var interest = 3.0;

This is because constants once declared remains same throughout the program and cannot be declared once again

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s