Azevedo Azevedo - 1 year ago 101
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 Source

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 = 'bob';
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download