0

I just started porting my JS application to modern (modular) JavaScript. I am not very expert in this language, but in Python I normally have a file with many useful variables or costants which are shared (imported) among multiple other scripts within a project.

Is it a good idea to do it in JavaScript as well?

example: i might create a init.js file like:

export const USEFULVAR = "Hello World!";
export const ANOTHERVAR = "What am I doing here???";

and then I can call them in whatever other file like.

whaterverotherfile.js

import * as useful from './init'

useful.USEFULVAR; // "Hello World!"
umbe1987
  • 2,894
  • 6
  • 35
  • 63
  • 1
    Sure, why not. What applies to Python mostly applies to JS as well. If you consider them to be `useful`, they probably are. – Bergi Sep 18 '18 at 19:31
  • @Bergi Thanks for answering so fast. Do you know any sort of convention/best practice to name such file? – umbe1987 Sep 18 '18 at 19:33
  • 1
    Name them for their contents. And depending on your actual use case, you might want to use multiple files with constants that are relevant only for specific parts of your application - but that's a general principle of modularity, nothing specific to JS or modules of constants. Make the file as large as you are comfortable with. – Bergi Sep 18 '18 at 19:38

0 Answers0