JavaScript Constants



Now let’s declare variable for interest rate.

let interest Rate = 0.3;

In Variable Initial value we can change later.

Interest Rate = 1;

Console.log(Interest Rate);

Resigned variable value to 1.

So, in console It will not display 0.3. It will display interest Rate as 1

In real world applications sometimes, we don’t want value of the variable to change. Because otherwise it going to create all kind of bugs in your application in those situation. Instead of variable we use constant. Instead, of let we use const. So, the value of the variable as the name implies can change. But the value of the constant cannot change. So, change let to const

 const   InterestRate = 0.3;

Interest Rate = 1; Reassigned value console.log (interest rate); So, after saving if you check console, it will show error (uncaught type error; Assignment to constant variable) with line number of error other, so we cannot reassign the constant value.

so here is a best practice if you don’t like to reassign the value Constant (const) is your default choice Otherwise, if you need to reassign the value, then use variable (let).

Post a Comment

Share your thoughts here

Previous Post Next Post