Azevedo Azevedo - 1 month ago 22
Javascript Question

Node JS allowing constants to be changed

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';

Answer

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';