Open In App

How to share code between files in JavaScript ?

JavaScript is a powerful and popular programming language that is widely used for web development. One of the key features of JavaScript is its ability to share code between files. This can be useful for organizing large projects, reusing code, and maintaining code quality. In this article, we’ll see the different ways to share code between files in JavaScript explored.

Approaches: Below are the two different approaches for ES5 and ES6:



ES5 Approach:

 



ES6 Approach:

As you can see, both ES5 and ES6 approaches have their own benefits and drawbacks, and it’s important to understand the different options before deciding which approach to use in your application.

Example 1: Using ‘global’ variables/functions: A variable or function defined in the global scope (i.e., outside of any function or block), can be accessed from any file. However, it is considered a bad practice to use global data members as it might lead to naming conflicts and make code hard to read.




// Defining a global function
function sum(a, b) {
    return a + b;
}
  
// Defining another global function 
function multiply(a, b) {
    return a * b;
}




// Function calls
console.log(sum(4, 6)); 
console.log(multiply(4, 6));

Output:

10
24

Example 2: Using the ‘export’ and ‘import’ Keywords: You can use the export keyword to specify which code you want to share, and then use the import keyword to access it in another file. 




// Defining a function to be exported
function sum(a, b) {
    return a + b;
}
  
// Defining another function to be exported
function multiply(a, b) {
    return a * b;
}
  
// Exporting the functions
export { sum, multiply };




// Importing the exported functions 
// from the helper.js file
import { sum, multiply } from './helper.js';
  
// Function calls
console.log(sum(4, 6)); 
console.log(multiply(4, 6)); 

Output:

10
24

Example 3: Using module patterns: There are various patterns for organizing code in modules, such as the revealing module pattern and the Singleton pattern. These patterns can help you structure your code in a reusable way and hide implementation details.




// Defining a Helper module
const Helper = (function () {
  
    // Defining a function to be exported
    function sum(a, b) {
        return a + b;
    }
  
    // Defining another function to be exported
    function multiply(a, b) {
        return a * b;
    }
  
    // Returning the functions
    return {
        sum: sum,
        multiply: multiply,
    };
})();
  
// Exporting the Helper module
export default Helper;

main.js: Then, these functions may be called in another file, main.js as follows:




// Importing the Helper module
import Helper from './helper';
  
// Calling the imported functions
console.log(Helper.sum(4, 6));
console.log(Helper.multiply(4, 6));

Output:

10
24

In conclusion, there are a number of ways to share code between files in JavaScript. The most modern and recommended approach is to use the export and import keywords, which are part of the ECMAScript module system. This allows you to specify which code you want to share and access it from other files in a clear and concise way.

Alternatively, you can use module patterns, such as the revealing module pattern or the Singleton pattern, to organize your code in a reusable way. However, these approaches can be more verbose and may require more setup. You can also use global variables to share code, but this is generally not recommended as it can lead to naming conflicts and make it harder to understand how your code works.


Article Tags :