To export a constant from a module, we make use of the export
keyword followed by the keyword const
. Let’s say we have a module named constants.js
that contains a constant named API_URL
. We can export it like this:
// constants.js
export const API_URL = "https://api.example.com";
In this example, we are exporting the API_URL
constant with the value "https://api.example.com"
.
Now, to import this constant in another module, we use the import
keyword followed by the variable name we want to assign the imported value to. Let’s say we want to import the API_URL
constant into a module named api.js
. We can do it like this:
// api.js
import { API_URL } from './constants.js';
// Use the imported constant
console.log(API_URL);
In this example, we import the API_URL
constant from the constants.js
module using the { }
syntax. We specify the relative path to the constants.js
file using './constants.js'
.
Subsequently, we can use the imported API_URL
constant as we would any other variable.
It’s important to note that the export
and import
statements are part of the ES6 (ECMAScript 2015) module system and may not be supported in older browsers. To use these features, you may need to transpile your code using a tool like Babel, or use a bundler like Webpack.
In conclusion, exporting and importing constants in JavaScript modules is a convenient way to share and reuse constants across different parts of your application. It promotes modularity and helps maintain a clean and organized codebase.
#javascript #modules