4
\$\begingroup\$

I have written this function to create a URL:

function createUrl(urlPath) {
    const endpoint = url.parse(env.config.get('myEndpoint'));
    endpoint.pathname = path.posix.join(endpoint.pathname, urlPath);
    return endpoint;
}

const basically creates a mutable object whose properties can still be modified. Here, I need to declare some Object which will only be modified once. Am I correct in declaring endpoint as const?

\$\endgroup\$
  • 1
    \$\begingroup\$ According to me in the field of programming Constant means, in fact, a constant which should never be modified. Now in Javascript I don't understand why but I can modify an object declared as constant. But shouldn't it be that we should only declare a constant when we know for sure it will never be modified? Having a constant and then modifying it directly afterwards is misleading isn't it? PS:- Me and my co-worker had a discussion on this topic and he seems adamant on the fact that JS constants are meant to be used like in the code above. I need your opinions as well on this. \$\endgroup\$ – abhinav Jul 30 at 20:14
  • 1
    \$\begingroup\$ Yeah, it's ok. All const means in JS is you can't reassign that "variable" to anything else. It doesn't affect mutability of properties (you'd use property descriptors for that). It definitely seems weird at first, but you can't necessarily take meanings from one language and project them onto another. \$\endgroup\$ – user11536834 Jul 30 at 20:37
  • 1
    \$\begingroup\$ See also Object.freeze and Object.seal. \$\endgroup\$ – user11536834 Jul 30 at 21:09
  • \$\begingroup\$ @user11536834 I was more concerned about the readability of the code. By name const means something that you are not planning on changing. Declaring an object as a constant and then updating in the very next line seemed a bit odd to me. Is there any performance drawback if I used a let here instead? \$\endgroup\$ – abhinav Jul 30 at 21:42
  • \$\begingroup\$ I'd guess the JIT would be smart enough to figure out that you weren't going to change it anyway with a let in a case like this, but you never know. Annoyingly, this seems to lead to a situation where you "should" use const instead of let most of the time, because you usually won't be reassigning the value. IMO that goes against the "nature" of JS, because historically it tends to favor robustness over pickiness (e.g. implicit conversion, weak typing, etc.). But that's just my opinion; extremely strict languages are a popular thing now days. \$\endgroup\$ – user11536834 Jul 30 at 21:56
2
\$\begingroup\$

Yes, it's correct to use const here. That would be considered idiomatic Javascript. If you're using const when you shouldn't, you'll notice as it will produce a TypeError.

People coming from other languages might disagree as they think that const means that the variable is constant/immutable (which is not the case, it's just not re-assignable). But that is really a critique of the naming of the keyword, not your usage of the keyword (which is correct).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.