Chris Wheatley

Software engineer with a passion for JavaScript, React & Progressive Web Apps. Currently building awesome products at Sky UK.

Keeping Your consts Constant

  • javascript, es6, es2015

The only way to ensure you're still importing a read only value (const) with ES2015's new module system is to explicitly import the value.

consts.js
export const VERSION = '1.0.0'  
export const PORT = 3000  
index.js
import {VERSION, PORT} from './consts'

VERSION = '1.1.0'  
// this won't work as the value imported is still a const

If you were to import any other way, you're creating a brand new object and assigning all the exported values to it, thus losing their read only-ness.

index.js
import * as consts from './consts'

consts.VERSION = '1.1.0'  
// unlike the example above this will work as the value is now an object property

This pattern results in a cleaner import but you're losing the read only benefits of defining the const in the first place.

You're also leaving the value open to be amended, either accidently or not, at a later point which could result in unexpected behaviour and wasted time debugging.

comments powered by Disqus