Const in JavaScript when to use it and is it necessary

0 votes

In JavaScript, I recently came across the const keyword. It appears to be used to create immutable variables, and I checked it to guarantee that it cannot be redefined (in Node.js):

const x = 'const';
const x = 'not-const';

// Will give an error: 'constant 'x' has already been defined'


I understand that it isn't yet standardized across all browsers - but I'm primarily interested in the context of Node.js V8, and I've seen that some developers/projects appear to favor it excessively when the var keyword may achieve the same result.

1. When should const be used in place of var?
2. Should it be used whenever a variable that will not be re-assigned is declared?
3. Does it make a difference if var is used instead of const or vice versa?

Dec 8, 2022 in Java by Nicholas
• 7,760 points
641 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes
Your questions have two parts: what are the technical implications of using const instead of var, and what are the human

 implications.

The technical distinction is crucial.

In compiled languages, a constant is substituted at build time, allowing for various optimizations such as dead code elimination to further improve the code's runtime performance.

Recent (loosely defined) JavaScript engines actually compile JS code to improve efficiency, thus utilising the const keyword would alert them that the above-mentioned optimizations are available and should be performed. This leads to improved performance.

The semantics of the term are the human-related aspects.

A variable is a data structure that stores information that is likely to change.

A constant data structure stores information that will never change. Var should always be used when there is space for mistakes.

However, not all information that never changes over a program's lifetime must be declared with const.

If the information should change under various situations, use var to indicate that, even if the actual change does not occur in your

code.
answered Dec 10, 2022 by Damonlang
• 1,230 points

edited Mar 5

Related Questions In Java

0 votes
0 answers

What is TypeScript and why would I use it in place of JavaScript?

Could you kindly explain the TypeScript language? What ...READ MORE

Nov 16, 2022 in Java by Nicholas
• 7,760 points
460 views
0 votes
2 answers

What is the use of toString method in Java and how can I use it ?

Whenever you require to explore the constructor ...READ MORE

answered Aug 23, 2018 in Java by Daisy
• 8,140 points
4,307 views
0 votes
2 answers

What is the use of @Override annotation in Java ? When do we use it ?

@Override annotation is used when we override ...READ MORE

answered Aug 14, 2019 in Java by Sirajul
• 59,230 points
3,694 views
0 votes
2 answers

When to use LinkedList and ArrayList in Java?

ArrayList is what you want. LinkedList is almost always a ...READ MORE

answered Dec 11, 2018 in Java by Sushmita
• 6,920 points
1,283 views
0 votes
2 answers

When and how to use Super() keyword in Java?

super() is used to call immediate parent. super() can be ...READ MORE

answered Jul 9, 2018 in Java by Sushmita
• 6,920 points
2,005 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
2,062 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,596 views
0 votes
1 answer

Start script missing error when running npm start

It seems that there is an undefined ...READ MORE

answered Feb 10, 2022 in Java by Soham
• 9,710 points
4,597 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP