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';
- Importing Named Exports:
// file.js
module.exports = 'John';// main.js
const name = require('./file.js');
console.log(name); // JohnES6 introduced a new syntax for exporting and importing modules.
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 30Default 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