Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5997101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:12:53+00:00 2026-05-23T00:12:53+00:00

I have some constants in JavaScript that I’d like to reuse in several files

  • 0

I have some constants in JavaScript that I’d like to reuse in several files while saving typing, reducing bugs from mistyping, keeping runtime performance high, and being useful on either the node.js server scripts or on the client web browser scripts.

example:

const cAPPLE = 17;
const cPEAR = 23;
const cGRAPE  = 38;
...(some later js file)...
for...if (deliciousness[i][cAPPLE] > 45) ...

Here are some things I could do:

  1. copy/paste const list to top of each file where used. Oh, Yuck. I’d rather not. This is compatible with keeping the constant names short and simple. It violates DRY and invites all sorts of awful bugs if anything in the list changes.

  2. constant list —> const.js

on browser, this is FINE … script gets fed in by the html file and works fine.

but on node.js, the require mechanism changes the constant names, interfering with code reuse and requiring more typing, because of how require works….

AFAIK This doesn’t work, by design, in node.js, for any const.js without using globals:

require('./const.js');
for...if...deliciousness[i][cAPPLE] > 45 ...;  

This is the node.js way:

(... const.js ....)
exports.APPLE = 17;
(... dependency.js ... )
var C = require('./const.js');
for...if...deliciousness[i][C.APPLE] > 45..... 

so I would either have to have two files of constants, one for the node.js requires and one for the browser, or I have to go with something further down the list…

3 make the constants properties of an object to be imported … still needs two files… since the node.js way of importing doesn’t match the browser. Also makes the names longer and probably takes a little more time to do the lookups which as I’ve hinted may occur in loops.

4 External constant list, internal adapter…. read the external constants, however stored, into internal structure in each file instead of trying to use the external list directly

const.js
exports.cAPPLE = 17

browser.js
const cAPPLE = exports.cAPPLE;
...code requiring cAPPLE...

node.js
CONST = require(./const.js)
const cAPPLE = CONST.cAPPLE;
...code requiring cAPPLE...

This requires a one-time-hit per file to write the code to extract the constants back out, and so would duplicate a bunch of code over and over in a slightly more evolved cut and paste.

It does allows the code requiring cAPPLE to continue to work based on use of short named constants

Are there any other solutions, perhaps a more experienced JavaScripter might know, that I might be overlooking?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T00:12:54+00:00Added an answer on May 23, 2026 at 12:12 am

    I would just make them global keys:

    ...(module consts.js)...
    
    global.APPLE = 17;
    global.PEAR = 23;
    global.GRAPE  = 38;
    
    ...(some later js file)...
    
    var C = require('./const.js');
    
    for (var i = 0; i < something.length; i++) {
        if (deliciousness[i][global.APPLE] > 45) { blah(); }
    }
    

    They wouldn’t be enforced constants, but if you stick to the ALL_CAPS naming convention for constants it should be apparent that they shouldn’t be altered. And you should be able to reuse the same file for the browser if you include it and use it like so:

    var global = {};
    <script src="const.js"></script>
    <script>
        if (someVar > global.GRAPE) { doStuff(); }
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some constants that represent the valid options in one of my model's
Possible Duplicate: Avoiding repeated constants in CSS We have some theme colors that are
I have a lot of constants that are somehow related, at some point I
I have a JavaScript object that looks like the following: venue = function(map, dataSet)
I have a bunch of javascript functions which depend on some server-side constants (such
I would like to have some Javascript code running in a web browser write
I have a javascript that contacts a php page which gets some data from
I have some UI in VB 2005 that looks great in XP Style, but
We have some input data that sometimes appears with &nbsp characters on the end.
I have some classes layed out like this class A { public virtual void

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.