11

If creating a reference to an object, and the reference is not going to change (even though the object will), is it better to use const instead of var?

For example:

const moment = require('moment') exports.getQuotation = function(value) { const quotation = {}; quotation.value = value; quotation.expiryDate = moment().add(7, 'days'); // Do some other stuff with quotation perhaps return quotation; }; 
2
  • I would not mess with const, is ES6, it will not increment your code performance but it will make a bit more difficult to fix and refactor. Stick with var (always declare you variables) Commented Sep 24, 2014 at 11:41
  • 1
    @donnanicolas ES6 constand let are pretty much final Commented Sep 24, 2014 at 12:10

3 Answers 3

4

You can use const, but you have to run node on --harmony

node app.js --harmony 

You also have to set "use strict", otherwise you'll have run-time execution issues:

exports.getQuotation = function(value) { "use strict"; const I_AM_CONSTANT = {}; let quotation = {}; ... 

Other than that, yes if you are running node on --harmony, semantically it makes more sense to use const for constants. For actual variables you should use let instead of var, as let only defines the variable in the scope (avoids hoisting issues)

Sign up to request clarification or add additional context in comments.

Comments

3

If you check MDN, for its reference you can find that its an experimental technology which is part of ES6 proposal. Its browser support is limited.

Reference for const from MDN

This is an experimental technology, part of the Harmony (ECMAScript 6) proposal. Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future version of browsers as the spec changes.

So this answers whether you should use everywhere. :)

Also if you want to create properties in objects which should not be allowed to change, then you should check Object.defineProperties() and the use of writable attribute.

3 Comments

I don't think this is what the requestor meant. I think the question is "If I'm in an environment supporting const and let, which one should I prefer?" He says var, but I think the question is more about let which has the same block scope.
@Pipo : I am sorry. I got this from your comment. But where is it in the question?
I think it is this part "If creating a reference to an object, and the reference is not going to change (even though the object will), is it better to use const instead of foo?". I changed var to foo to make it clear that I think he doens't want a real comparison to var (or let), but looking at the first part, I think he wants to know, if it is generally good to use const for Objects, if their contents change.
1

const is used for a variable constant, i.e. once declared, it's value is not supposed to change, whereas var can be changed as per the will.

Use of const and var is solely on your will. For me it goes as follows

  • If I'm 100% sure that the value of a variable is not going to change and it's not going to re-declared, I'll define it as a const.
  • In all other cases I'll use var.

Mind it, re-declaring const will immediately throw an error.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.