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.export keyword do in JavaScript modules?exportexport keyword is used to share variables or functions from one file so other files can use them.import brings code in, and running code is separate; export only shares code.PI?export const PI = 3.14;.const PI = 3.14; export PI;) uses incorrect export syntax after declaration (needs export { PI };); B and D have invalid syntax.math.js:
export function add(a, b) {
return a + b;
}
export const number = 5;
What will be the output of this code in app.js?
import { add, number } from './math.js';
console.log(add(number, 10));add adds two numbers, and number is 5.add(5, 10) returns 15.utils.js?
function greet() {
return 'Hello';
}
export greet;export greet; is invalid syntax in JavaScript modules.export { greet };.export { greet }; -> Option Ccalculate, a constant PI, and a variable radius. Which is the best way to export all three at once?export { name1, name2, name3 };.Pass the test and this pattern moves unknown