create a config object in javascript

To create a config object in JavaScript, we can use an object literal syntax as follows:

const config = {
  port: 8080,
  timeout: 10000,
  apiKey: 'abc123',
  maxRetries: 3,
  debugMode: true
};

module.exports = config;
133 chars
10 lines

In the above example, we define an object that contains configuration options for our application, including a port number, a timeout value, an API key, a maximum number of retries, and a debug mode flag. We then use module.exports to make the config object available to other modules.

We can then use this config object in other parts of our code by importing it as follows:

const config = require('./config');

console.log(config.port); // 8080
console.log(config.timeout); // 10000
console.log(config.apiKey); // 'abc123'
console.log(config.maxRetries); // 3
console.log(config.debugMode); // true
225 chars
8 lines

In this example, we import the config object from a separate config.js file and use it to retrieve the various configuration options we defined earlier. This allows us to easily configure our application without having to hardcode values throughout our codebase.

gistlibby LogSnag