Skip to content

Latest commit

 

History

History
70 lines (52 loc) · 1.64 KB

File metadata and controls

70 lines (52 loc) · 1.64 KB

🔁 Export and Import

👀 Fast Lookup

  • export: Share code with another file.
    • Named export: Share multiple values or functions from a file.
    • Default export (export default): Share a single value or function from a file.
  • import: Get code from another file.
    • Importing Named Exports: import { name } from './file.js';
    • Importing Default Exports: import name from './file.js';

👵🏻 Before ES6

// file.js
module.exports = 'John';
// main.js
const name = require('./file.js');
console.log(name); // John

📦 Export and Import

ES6 introduced a new syntax for exporting and importing modules.

Named Export and Import

Named export is used when you want to export multiple values or functions from a file.

// file.js
export const name = 'John';
export const age = 30;

You can import named exports using curly braces.

// main.js
import { name, age } from './file.js';
console.log(name, age); // John 30

Default Export and Import

Default export is used when you want to export a single value or function from a file.

// file.js
const name = 'John';
export default name;

You can import a default export without curly braces.

// main.js
import name from './file.js';
console.log(name); // John