TalentJR

Lesson 11

ES6 Modules

Split code across import/export files

1What are ES6 Modules?✨

ES6 Modules are a way to organize and structure your JavaScript code by splitting it into separate files. This makes your code easier to manage and reusable across different parts of your application.

2Why Use Modules?🎯

  • ✓ Keeps your code organized and clean
  • ✓ Allows for better collaboration on larger projects
  • ✓ Makes it easier to reuse code
  • ✓ Enhances maintainability by isolating functionality

3Creating a Module💡

To create a module, you simply need to export functions, objects, or values from one file and import them into another. Here’s how you can do it:

In a file named `math.js`, you can export a function like this:

💻 code
export function add(a, b) {
    return a + b;
}

4Then, in another file, you can import and use this function:🚀

💻 code
import { add } from './math.js';
💻 code
console.log(add(5, 3)); // Outputs: 8

5Importing Modules🔍

You can import entire modules or specific parts of them. Here’s how to import everything from a module:

💻 code
import * as math from './math.js';
💻 code
console.log(math.add(5, 3)); // Outputs: 8

6Using Default Exports⭐

You can also export a single value as the default export. Here’s an example:

7In `greeting.js`:🧩

💻 code
export default function greet(name) {
    return `Hello, ${name}!`;
}

8And in your main file:🎈

💻 code
import greet from './greeting.js';

console.log(greet('Alice')); // Outputs: Hello, Alice!

9What You'll Build This Grade✨

  • ✓ Modular applications for better organization
  • ✓ Interactive web pages using multiple files
  • ✓ Reusable components for larger projects
  • ✓ Improved collaboration with peers on coding tasks

🎯 Practice Challenges

Master ES6 Modules by completing all 3 difficulty levels

🟢 EASY

ES6 Modules - Easy Challenge

Start with the basics. Perfect for beginners!

🟡 MEDIUM

ES6 Modules - Medium Challenge

Get more challenging. Build your confidence!

🔴 HARD

ES6 Modules - Hard Challenge

Master this concept. Prove your skills!

🏆 Study a lesson for 2+ minutes to earn +10 TalentJR Points a day. Badges, certificates & prizes →

Ready to Master ES6 Modules? 🚀

Complete the challenges, take the quiz and keep your learning streak going!