Modules (ES Modules)

 Modules (ES Modules)


Modules allow you to organize your code into reusable and separate files. ES modules provide a standardized way of working with modules in JavaScript.



// math.js

export function add(a, b) {

  return a + b;

}


export function subtract(a, b) {

  return a - b;

}


// main.js

import { add, subtract } from './math.js';


console.log(add(5, 3)); // Output: 8

console.log(subtract(10, 4)); // Output: 6

In the example above, the math.js file exports two functions (add and subtract) using the export keyword. In the main.js file, you can import these functions using the import keyword and use them in your code.

No comments:

Post a Comment

The Importance of Cybersecurity in the Digital Age

 The Importance of Cybersecurity in the Digital Age Introduction: In today's digital age, where technology is deeply intertwined with ev...