Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node JS allowing constants to be changed [duplicate]

Why does node.js allow a module (or an object) specified as a constant to be changed?

For example, this is allowed:

const EXPRESS = require('express');
EXPRESS.someProperty = 'some value';

But this is not:

const MYCONST = '123';
MYCONST = '456';
like image 340
Azevedo Avatar asked Nov 21 '25 08:11

Azevedo


2 Answers

const means that you cannot change the reference itself, not what the reference points to.

const a = { name: 'tom' };

// you cannot change the reference (point a to something else)
a = 5; // this is an error

// but you can change the value stored at that reference with no problem
a.name = 'bob';
like image 194
nem035 Avatar answered Nov 22 '25 21:11

nem035


const does not make the object to become immutable thus you can change the object itself but you can not assign another object to that reference

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/const

like image 27
Dmitry Matveev Avatar answered Nov 22 '25 21:11

Dmitry Matveev