Import And Export IN JavaScript

Import & Export In JavaScript
In JavaScript, there are two ways to import modules from other files: named imports and default imports.
A named import allows you to import specific exports from a module by name. To use a named import, you need to wrap the exported names in braces
{}when importing them. For example:// math.js export const add = (a, b) => a + b; export const subtract = (a, b) => a - b; // app.js import { add, subtract } from './math.js'; console.log(add(2, 3)); // output: 5 console.log(subtract(5, 2)); // output: 3In this example, we are importing the
addandsubtractfunctions from themath.jsmodule using named imports.On the other hand, a default import allows you to import a single export from a module without specifying its name. To use a default import, you simply import the module without braces. For example:
// math.js export default { add: (a, b) => a + b, subtract: (a, b) => a - b, }; // app.js import math from './math.js'; console.log(math.add(2, 3)); // output: 5 console.log(math.subtract(5, 2)); // output: 3In this example, we are exporting an object with
addandsubtractproperties as the default export from themath.jsmodule. We can then import this default export in theapp.jsfile by simply writingimport math from './math.js'. Here,mathis the name of the default export, and we can access its properties directly using dot notation (math.add,math.subtract, etc.).Note: that a module can have only one default export, but multiple named exports.