Home > Web Front-end > JS Tutorial > Understanding Exports and Imports in JavaScript

Understanding Exports and Imports in JavaScript

Mary-Kate Olsen
Release: 2024-11-03 05:51:30
Original
212 people have browsed it

Understanding Exports and Imports in JavaScript

In JavaScript, modules are self-contained units of code that can expose assets to other modules using export and consume assets from other modules using import. This mechanism is essential for building modular and reusable code in modern JavaScript applications.

Default Exports

  • A module can have only one default export.
  • To export a default asset, use the default keyword before the exported entity.
  • To import a default export, you can directly assign it to a variable without specifying the exported name:
// Exporting a default asset
export default function greet(name) {
  console.log(`Hello, ${name}!`);
}

// Importing the default export
import greet from './myModule';
Copy after login

Named Exports

  • A module can have multiple named exports.
  • To export a named asset, use the export keyword before the exported entity and give it a name.
  • To import named exports, you must specify the names of the assets you want to import:
// Exporting named assets
export function greet(name) {
  console.log(`Hello, ${name}!`);
}

export function farewell(name) {
  console.log(`Goodbye, ${name}!`);
}

// Importing named exports
import { greet, farewell } from './myModule';
Copy after login

Combining Default and Named Exports

You can have both a default export and named exports in a single module:

export default function greet(name) {
  console.log(`Hello, ${name}!`);
}

export function farewell(name) {
  console.log(`Goodbye, ${name}!`);
}
Copy after login

To import both the default and named exports:

import greet, { farewell } from './myModule';
Copy after login

Key Points to Remember

  • export is used to expose assets from a module.
  • import is used to consume assets from other modules.
  • A module can have only one default export.
  • Named exports can be imported individually or collectively.
  • The names used for imports are arbitrary and don't have to match the names used in the exported module.

Practical Example

Consider a React component:

import React from 'react';

export default function Greeting({ name }) {
  return <h1>Hello, {name}!</h1>;
}
Copy after login

In this example, the Greeting component is exported as the default export. It can be imported and used in other components:

import Greeting from './Greeting';

function MyComponent() {
  return <Greeting name="Alice" />;
}
Copy after login

By understanding exports and imports, you can effectively organize and reuse code in your JavaScript projects.

The above is the detailed content of Understanding Exports and Imports in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template