1 min read

Exports

exports is a special keyword in JavaScript that is used to define a module’s exports. It is a read-only object that contains the properties that are exposed by the module to the global scope.

Syntax:

exports = { // Properties: prop1: value1, prop2: value2, ...};

Example:

exports = { name: 'John Doe', age: 30, sayHello: function() { console.log('Hello, ' + this.name); }};

In this example:

  • exports is an object that has three properties: name, age, and sayHello.
  • The name property has a value of John Doe.
  • The age property has a value of 30.
  • The sayHello property is a function that prints “Hello, ” followed by the value of the name property to the console.

To use the exports object:

“`const exports = require(‘./module’);

console.log(exports.name); // Output: John Doeconsole.log(exports.sayHello()); // Output: Hello, John Doe“`

Note:

  • The exports object is read-only, meaning you cannot modify its properties directly.
  • You can add new properties to the exports object by assigning them to the object.
  • You can remove properties from the exports object by deleting them from the object.

Additional Information:

  • The exports object is used in modules to define the exports that are available to other modules.
  • Modules are loaded using the require() function, and the exports object is accessible through the require() object.
  • The exports object is a common way to share data between modules.

Disclaimer