Concept Flow - Exporting values
Define value or function
Export using export keyword
Import in another file
Use the exported value
You create a value or function, export it, then import it elsewhere to use it.
export const greeting = "Hello"; // In another file import { greeting } from './file.js'; console.log(greeting);
| Step | Action | Code Line | Result/Output |
|---|---|---|---|
| 1 | Define constant greeting | const greeting = "Hello"; | greeting = "Hello" |
| 2 | Export greeting | export { greeting }; | greeting is available to import |
| 3 | Import greeting in another file | import { greeting } from './file.js'; | greeting imported successfully |
| 4 | Print greeting | console.log(greeting); | Hello |
| 5 | End | No more code | Program ends |
| Variable | Start | After Export | After Import | Final |
|---|---|---|---|---|
| greeting | undefined | "Hello" | "Hello" | "Hello" |
Exporting values in JavaScript:
- Use 'export' before variables/functions to share them.
- Import them in other files with 'import { name } from "file"'.
- Exported values can be constants, functions, or objects.
- Without import, exported values are not accessible.
- Multiple exports allowed per file.