About Lesson
Importing in JavaScript Modules?
Modules allow you to export variables, functions, or classes from one file so they can be imported into another.
Import Syntax:
- Named Import: import { name1, name2 } from ‘./module’;
- Default Import: import defaultName from ‘./module’;
- Renaming Imports: Use as to rename imports.
- Import Everything: Use * to import all named exports.
Import Everything Example:
File: mathUtils.js
JavaScript
export const add = (a, b) => a + b;
export const subtract = (a, b) => a - b;
export const multiply = (a, b) => a * b;
File: main.js
JavaScript
import * as MathUtils from './mathUtils.js';
console.log(MathUtils.add(2, 3)); // Output: 5
console.log(MathUtils.subtract(10, 7)); // Output: 3