Lesson 6 of 30
ES Modules
20% complete
Chapters
Chapter 6
ES Modules
Learn JavaScript ES Modules and import/export syntax in Node.js.
Theory
ES Modules are the standard JavaScript module system.
They use:
export
import
ES Modules provide a modern way to organize JavaScript applications.
Node.js supports ES Modules when the project is configured appropriately, such as using the .mjs extension or setting the module type in package.json.
Example
Node.js Example
// math.js
export function add(a, b) {
return a + b;
}
// app.js
import { add } from "./math.js";
console.log(add(10, 5));Output
The add function is imported from the ES module and returns 15.
Practice
Create an ES module that exports two mathematical functions.
Interview Questions
1.
What are ES Modules?
2.
What is the difference between import and require?
3.
How do you export a function using ES Modules?
Quick Quiz
Which option best matches this chapter?
A.import and export
B.require and SELECT
C.include and define
D.fetch and render
Answer: import and export