Exporting from a Module

Learn the different ways of exporting from a module in JavaScript.

Primitives, functions, objects, and classes defined within a module are visible to the outside only if exported. JavaScript offers several options for exporting; choose the simplest option that meets your needs in a given situation.

Inlining exports

You may declare a reference or a class and, at the same time, export it, that is, inline the export keyword in the declaration. This approach is the easiest and least noisy approach to exporting.

To inline an export, prefix a declaration with the export keyword, which makes the declared reference available outside the module.

📍 The name of the declared reference becomes the name that is exported; this is called a named export.

Example

For example, the following temperature module exports a primitive, a function, an object, and a class, with their respective names. The primitive referenced by the FREEZINGPOINT_IN_F variable is not exported and is visible only within the temperature module/file.

Get hands-on with 1200+ tech skills courses.