JavaScript / JavaScript Modules and Import/Export
Using Import and Export Statements
In this tutorial, you will gain a deep understanding of how to use import and export statements in JavaScript. This includes importing and exporting functions, objects, or values …
Section overview
5 resourcesIntroduces JavaScript modules and the import/export syntax for better code organization.
1. Introduction
This tutorial aims to provide a comprehensive understanding of how to use import and export statements in JavaScript. These are powerful features that allow you to split your code into multiple, reusable modules, making your code cleaner and easier to maintain.
By the end of this tutorial, you will:
- Understand the syntax and usage of import and export statements.
- Know how to import and export functions, objects, or values from modules.
- Learn best practices when using these statements.
Prerequisites: Basic knowledge of JavaScript syntax and concepts are required for this tutorial.
2. Step-by-Step Guide
In JavaScript, you can separate code into different files (modules) for better maintainability. To share functions, objects, or primitive values between these modules, you can use export to share them, and import to bring them in.
Exporting:
There are two types of exports: named exports (zero or more exports per module) and default exports (one per module).
- Named exports: They can be exported as they are declared, or all together in an export statement.
// Export as declared
export const pi = 3.14159;
// All together
const gravity = 9.8;
const speedOfLight = 299792458;
export { gravity, speedOfLight };
- Default exports: They are particularly useful if a module only exports one thing, like a class or function.
export default function() { console.log('Hello World!'); }
Importing:
- Named imports: To import specific exports from a module, import them by name inside curly braces
{}.
import { pi, gravity } from './constants.js';
- Default imports: Default exports are imported without curly braces
{}.
import greet from './greet.js';
3. Code Examples
Let's look at some practical examples:
Example 1: Named Exports
constants.js
// Exports named constants
export const pi = 3.14159;
export const gravity = 9.8;
main.js
// Import named exports from "constants.js"
import { pi, gravity } from './constants.js';
console.log(pi); // Outputs: 3.14159
console.log(gravity); // Outputs: 9.8
Example 2: Default Export
greet.js
// Default export
export default function() { console.log('Hello World!'); }
main.js
import greet from './greet.js';
greet(); // Outputs: Hello World!
4. Summary
In this tutorial, you've learned how to use import and export to share code between JavaScript modules. Keep in mind that default exports are useful when a module only wants to export a single class or function, while named exports can be used to export several values.
For further learning, consider exploring how to use import and export with classes, as well as understanding the difference between importing a module vs. importing a module's content.
5. Practice Exercises
-
Create two modules: one that exports a function (default) that returns the square of a number, and another that imports and uses this function.
-
Create a module that exports an object (named) with properties representing different mathematical constants. Create another module that imports and uses this object.
-
Create a module that exports a class (default) representing a geometric shape, and another module that imports this class and instantiates an object.
Solutions and detailed explanations will be provided, along with tips for further practice to strengthen your understanding of these concepts.
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article