Table of Contents
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.name property has a value of John Doe.age property has a value of 30.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:
exports object is read-only, meaning you cannot modify its properties directly.exports object by assigning them to the object.exports object by deleting them from the object.Additional Information:
exports object is used in modules to define the exports that are available to other modules.require() function, and the exports object is accessible through the require() object.exports object is a common way to share data between modules.Table of Contents
Categories