📖 Scenario: You are building a small JavaScript module that holds some data and you want to share this data with other parts of your program.
🎯 Goal: Create a JavaScript module that exports a value, then import and use that value in another file.
📋 What You'll Learn
Create a variable with a specific value
Export the variable using export
Import the exported variable in another file
Print the imported value to the console
💡 Why This Matters
🌍 Real World
Exporting and importing values is how JavaScript modules share code and data in real projects.
💼 Career
Understanding exports and imports is essential for working with modern JavaScript frameworks and libraries.
Progress0 / 4 steps
1
Create a variable to export
Create a variable called greeting and set it to the string 'Hello, world!'.
Javascript
Hint
Use const greeting = 'Hello, world!'; to create the variable.
2
Export the variable
Add a line to export the variable greeting using the export keyword.
Javascript
Hint
Use export { greeting }; to export the variable.
3
Import the exported variable
In a new file, write an import statement to import greeting from the module file named './greetings.js'.
Javascript
Hint
Use import { greeting } from './greetings.js'; to import the variable.
4
Print the imported value
Write a line to print the imported greeting variable to the console using console.log.
Javascript
Hint
Use console.log(greeting); to print the value.
Practice
(1/5)
1. What does the export keyword do in JavaScript modules?
easy
A. It makes variables or functions available to other files.
B. It imports code from other files.
C. It runs the code immediately.
D. It deletes variables after use.
Solution
Step 1: Understand the purpose of export
The export keyword is used to share variables or functions from one file so other files can use them.
Step 2: Differentiate from import and execution
import brings code in, and running code is separate; export only shares code.
Final Answer:
It makes variables or functions available to other files. -> Option A
Quick Check:
export shares code = A [OK]
Hint: Export means share code to other files [OK]
Common Mistakes:
Confusing export with import
Thinking export runs code
Believing export deletes variables
2. Which of the following is the correct syntax to export a constant named PI?
easy
A. const PI = 3.14; export PI;
B. export const PI = 3.14;
C. export = const PI = 3.14;
D. const PI = 3.14 export;
Solution
Step 1: Check correct export syntax
To export a variable while declaring it, use export const PI = 3.14;.
Step 2: Identify syntax errors in other options
const PI = 3.14; export PI; (const PI = 3.14; export PI;) uses incorrect export syntax after declaration (needs export { PI };); B and D have invalid syntax.
Final Answer:
export const PI = 3.14; -> Option B
Quick Check:
export const PI = 3.14; = C [OK]
Hint: Put export before variable or function declaration [OK]
Common Mistakes:
Placing export after variable declaration
Using incorrect export assignment syntax
Missing semicolons causing confusion
3. Given this code in 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));
medium
A. 15
B. 510
C. NaN
D. SyntaxError
Solution
Step 1: Understand imported values
The function add adds two numbers, and number is 5.
Step 2: Calculate the function call
Calling add(5, 10) returns 15.
Final Answer:
15 -> Option A
Quick Check:
5 + 10 = 15 [OK]
Hint: Add imported values as normal functions [OK]
Common Mistakes:
Treating number as string causing concatenation
Forgetting to import both values
Syntax errors from wrong import path
4. What is wrong with this code in utils.js?
function greet() {
return 'Hello';
}
export greet;
medium
A. Missing export keyword before function declaration
B. No error; code is correct
C. Incorrect export syntax; should be export { greet };
D. Function greet cannot be exported
Solution
Step 1: Identify export syntax error
The line export greet; is invalid syntax in JavaScript modules.
Step 2: Correct export syntax for existing functions
To export an already declared function, use export { greet };.
Final Answer:
Incorrect export syntax; should be export { greet }; -> Option C
Quick Check:
Export existing function with braces = D [OK]
Hint: Use export { name } for existing declarations [OK]
Common Mistakes:
Trying to export without braces
Confusing export declaration with export statement
Assuming export greet; is valid
5. You want to export multiple values from a file: a function calculate, a constant PI, and a variable radius. Which is the best way to export all three at once?
hard
A. export calculate, PI, radius;
B. export default calculate, PI, radius;
C. export const calculate, PI, radius;
D. export { calculate, PI, radius };
Solution
Step 1: Understand exporting multiple named values
To export several named values together, use export { name1, name2, name3 };.
Step 2: Eliminate incorrect syntax options
Options A, C, and D are invalid syntax; A lacks braces, C misuses export const for multiple, D is for default export only.
Final Answer:
export { calculate, PI, radius }; -> Option D
Quick Check:
Use braces to export multiple names = B [OK]
Hint: Use export { a, b, c } to export many values [OK]
Common Mistakes:
Trying to export multiple values without braces
Using export default for multiple named exports
Mixing const and export syntax incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.