Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (13.1k points)
Can anyone help me with where to use Const keyword? And why it is better to use over var?

1 Answer

0 votes
by (26.7k points)

Basically, the const keyword helps to prevent the variable to assign to another value. In simple terms, it will make the pointer immutable, but the value remains mutable. It is better because it cannot be reassigned and also make code easier to reason. Below is the example:

const somethingConst = 'some default value'

// A lot of code here...

console.log(somethingConst)

// You know what the code above will print

// without going through all the previous code.

// You just know it, since the variable cannot be changed

I hope this will help.

Want to become a Java expert? Join Java Certification now!!

Browse Categories

...