ES6 Modules - why named const exports are not read only ES6 Modules - why named const exports are not read only javascript javascript

ES6 Modules - why named const exports are not read only


The other answer is incorrect.

The (theoretical) question is why in the first case it's possible to override the const value?

This is actually entirely independent of const. With ES6 module syntax, you are not allowed to reassign the exported value of a module, from outside the module. The same would be true with export let FOO; or export var FOO;. Code inside the module is the only thing that is allowed to change exports.

Doing settings.FOO = 1 technically should throw an exception, but most compilers don't handle this particular edge case currently.

As an example, you could do

export var FOO;export function setFoo(value){  FOO = value;}

and given this, this is when const becomes useful because it's the same as any other normal JS code. FOO = value would fail if it was declared as export const FOO, so if your module is exporting a bunch of constants, doing export const FOO = 1, FOO2 = 2; is a good way to export constants, it's just that Babel doesn't actually make them immutable.


In this code

import * as settings from './settings';settings.FOO = 1;

In the above code, you are not assigning directly to the constant variable but a cloned copy in settings.

import * as settings from './settings';         ^^^^^^^^^^^^settings.FOO = 1;

But it is not the case in the next code

import {FOO, BAR} from './settings'FOO = 1;

Here FOO and BAR are constants and you can't assign to it.