📖 Scenario: Imagine you are building a simple web app that shows a greeting message and calculates the sum of two numbers. To keep your code clean and organized, you want to separate these two tasks into different files called modules.
🎯 Goal: You will create two JavaScript modules: one for greeting and one for addition. Then you will use these modules in a main file to show the greeting and the sum. This will help you understand why modules are used to organize code better.
📋 What You'll Learn
Create a module file named greet.js that exports a function called sayHello which returns the string 'Hello, friend!'.
Create a module file named math.js that exports a function called add which takes two numbers and returns their sum.
Create a main file named main.js that imports sayHello from greet.js and add from math.js.
In main.js, call sayHello and print the result, then call add with numbers 5 and 7 and print the result.
💡 Why This Matters
🌍 Real World
In real projects, modules help developers work on different parts of a program separately and combine them easily.
💼 Career
Understanding modules is essential for JavaScript developers to write clean, maintainable code and collaborate with others.
Progress0 / 4 steps
1
Create the greeting module
Create a file named greet.js and write a function called sayHello that returns the string 'Hello, friend!'. Export this function using export function sayHello() syntax.
Javascript
Hint
Use export function sayHello() { return 'Hello, friend!'; } to create and export the function.
2
Create the math module
Create a file named math.js and write a function called add that takes two parameters a and b and returns their sum. Export this function using export function add(a, b) syntax.
Javascript
Hint
Use export function add(a, b) { return a + b; } to create and export the function.
3
Import modules in main file
Create a file named main.js. Import the sayHello function from greet.js and the add function from math.js using import { sayHello } from './greet.js' and import { add } from './math.js'.
Javascript
Hint
Use import { sayHello } from './greet.js'; and import { add } from './math.js'; to bring in the functions.
4
Use imported functions and print results
In main.js, call the sayHello() function and print its result using console.log. Then call the add(5, 7) function and print its result using console.log.
Javascript
Hint
Use console.log(sayHello()); and console.log(add(5, 7)); to show the results.
Practice
(1/5)
1. Why do JavaScript modules help in programming?
easy
A. They split code into smaller, manageable parts.
B. They make the code run faster.
C. They remove the need for variables.
D. They automatically fix bugs.
Solution
Step 1: Understand the purpose of modules
Modules are designed to divide code into smaller pieces that are easier to manage.
Step 2: Compare options with module benefits
Only splitting code into smaller parts matches the main use of modules.
Final Answer:
They split code into smaller, manageable parts. -> Option A
Quick Check:
Modules = smaller code parts [OK]
Hint: Modules break code into parts for easy management [OK]
Common Mistakes:
Thinking modules speed up code automatically
Believing modules remove variables
Assuming modules fix bugs by themselves
2. Which of the following is the correct way to export a function named greet from a JavaScript module?
easy
A. export greet() {}
B. export function greet() {}
C. function export greet() {}
D. export = greet()
Solution
Step 1: Recall JavaScript export syntax
The correct syntax to export a function is to write export function functionName() {}.
Step 2: Check each option
Only export function greet() {} matches the correct syntax for exporting a function.
Final Answer:
export function greet() {} -> Option B
Quick Check:
Export function syntax = export function name() [OK]
Hint: Use 'export function name()' to export functions [OK]
Common Mistakes:
Placing export after function keyword
Using incorrect export assignment syntax
Omitting the function keyword
3. What will be the output of the following code?
// file: math.js
export function add(a, b) { return a + b; }
// file: main.js
import { add } from './math.js';
console.log(add(3, 4));
medium
A. 34
B. undefined
C. Error: add is not defined
D. 7
Solution
Step 1: Understand import and export usage
The function add is exported from math.js and imported correctly in main.js.
Step 2: Calculate the function output
Calling add(3, 4) returns 3 + 4 = 7, which is logged.
Final Answer:
7 -> Option D
Quick Check:
3 + 4 = 7 [OK]
Hint: Imported functions run normally if exported correctly [OK]
Common Mistakes:
Forgetting to export the function
Using wrong import syntax
Expecting string concatenation instead of addition
4. Identify the error in this module usage:
// file: utils.js
export function greet() { console.log('Hello'); }
// file: app.js
import greet from './utils.js';
greet();
medium
A. Missing curly braces in import statement
B. Function greet is not exported
C. Wrong file path in import
D. greet is called without parentheses
Solution
Step 1: Check export and import syntax
The function greet is exported as a named export, so import must use curly braces.
Step 2: Identify import statement error
Importing without curly braces tries to import a default export, which does not exist here.
Final Answer:
Missing curly braces in import statement -> Option A
Quick Check:
Named exports need braces in import [OK]
Hint: Use braces for named imports, no braces for default [OK]
Common Mistakes:
Confusing named and default exports
Calling function without parentheses
Incorrect file path usage
5. You want to organize a large JavaScript project by splitting code into modules. Which of these is NOT a benefit of using modules?
hard
A. They help avoid naming conflicts by keeping variables local.
B. They allow code reuse across different files.
C. They automatically optimize code performance.
D. They make code easier to maintain and understand.
Solution
Step 1: Review benefits of modules
Modules help organize code, avoid conflicts, and allow reuse and maintenance.
Step 2: Identify the incorrect statement
Modules do not automatically optimize performance; optimization is separate.
Final Answer:
They automatically optimize code performance. -> Option C
Quick Check:
Modules organize, not optimize code [OK]
Hint: Modules organize and reuse code, not speed it up automatically [OK]
Common Mistakes:
Assuming modules improve speed automatically
Confusing code organization with performance
Ignoring variable scope benefits
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.