Importing and exporting between files

Importing and exporting between files

JavaScript programming has a powerful special attribute feature in JavaScript programming, launched in ECMAScript 2015, which helps JavaScript programmers to apply a program source code by dividing it into separate files and arranging them in a particular order using them as small program source code modules. Whereas modular programming approach or overview helps programmers to export program source code from one file and import it into another file. This makes it easier for programmers to maintain the program source codebase and handle the creation of large programs.

Importing and exporting between files

So, let’s get to know more about file import and export features in JavaScript programming.

Setting up your custom project structure in JavaScript.

In JavaScript programming, a programmer has a project, which contains the main files that are required for the project. For example,

/project-directory

├── /math.js // This file contains the module functions to be exported

├── /main.js // Here we import and use the module functions from math.js file

└── index.html // This is the main HTML file to run the project in the current program.

Exporting from a Module (File) in JavaScript.

Here in the program, in math.js file, the programmer defines some user defined functions, and exports them to the module functions by applying the named export or default export method in popular JavaScript.

Named export features in JavaScript.

So, let’s get to know the program of a named export method in JavaScript program better.

// math.js

export const total = (p, q) => p + q;

export const minus = (p, q) => p – q;

export const product = (p, q) => p * q;

export const division = (p, q) => p / q;

Here in this program, the programmer has applied many functions, which include total, minus, product, division, and these have been exported separately when needed.

Default export features in JavaScript.

In JavaScript programming, any single value such as a function or class object can also be exported in the default export order. For example,

// math.js

export default function product(p, q) {

return p * q;

}

In this program, the product variable has been exported in the default export order, due to which it can be imported by applying any name in another file.

Importing a file in another file in JavaScript.

Here, the JavaScript programmer imports the named exports module from math.js into main.js in the program.

Importing named exports in JavaScript.

If the JavaScript programmer has more than one named exports module exporting functions, the programmer can import them using the named exports wrapped in curly braces symbol.

// main.js

import { total, minus, product, division } from ‘./math.js’;

console.log(total(4, 4)); // Result – 8

console.log(minus(10, 7)); // Result – 3

console.log(product(4, 4)); // Result – 16

console.log(division(30, 3)); // Result – 10

Here, the program imports total, minus, product, and division into named functions from math.js.

Importing default exports in JavaScript.

When importing default exports in a JavaScript program, programmers do not apply the curly braces symbol, where programmers can give it any name.

/ main.js

import product from ‘./math.js’; // default import

console.log(product(24, 2)); // Result – 12

In this program, applying the product value is an example of default exports from math.js, and it can be imported into the program without the curly braces operator. Here programmers can select any name for the imported value.

Combining example of default and named imports in JavaScript.

JavaScript programmers can import both named exports and default export methods in the same program function module in the same program function statement.

// main.js

import product, { total, minus } from ‘./math.js’;

console.log(total(1, 2)); // Result is – 3

console.log(minus(7, 3)); // Result is – 4

console.log(product(3, 3)); // Result is – 9

Here in this program example, programmers import total and minus named exports and product default export from math.js.

Renaming imports in JavaScript.

JavaScript programmers can also rename named imports in the program by applying the as keyword.

// main.js

import { total as sum, minus as diff } from ‘./math.js’;

console.log(sum(4, 11)); // Result is – 15

console.log(diff(9, 4)); // Result is – 5

Here in this program the programmer changes the name of total to sum and subtract during the import process and changes or replaces the name of diff program variable.

Example of named exports, default exports, all together in a JavaScript program.

How JavaScript programmers use named exports and default exports all together in a program. Here programmer creates two files. Math.js file is for function export and main.js file is for import.

JavaScript math.js (export module).

// Example of Named Exports

export const total = (p, q) => p + q;

export const minus = (p, q) => p – q;

// Example of Default Export

export default function product(p, q) {

return p * q;

}

// JavaScript Default Export

main.js (Module with Imports):

// here we use Importing both named and default exports in same program

import product, { total, minus } from ‘./math.js’;

console.log(total(4, 4)); // Result is – 8

console.log(minus(9, 2)); // Result is – 7

console.log(product(2, 2)); // Result is – 4

Summary of import and export between files in JavaScript programming.

  • Named export features in JavaScript – JavaScript programmers can export multiple values ​​under multiple names by using the export keyword.
    • export const total = (p, q) => p + q;
  • Default export features in JavaScript – JavaScript programmers can export a value as a default export.
    • export default function product(p, q) { return p * q; }
  • Importing named exports in JavaScript – JavaScript programmers can apply curly braces to import named exports.
    • import { total, minus } from ‘./math.js’;
  • Importing default exports in JavaScript – In this, JavaScript programmer can import default exports without curly braces.
    • import multiply from ‘./math.js’;
  • Combining both imports in JavaScript – JavaScript programmer can import both default and named exports in a single statement.
    • import multiply, { total, minus } from ‘./math.js’;
  • Renaming imports in JavaScript – JavaScript programmer can rename named imports by applying as keyword.
    • import { total as sum } from ‘./math.js’;
  • Dynamic imports in JavaScriptJavaScript programmer can import modules asynchronously as per requirement by using import() function in the program.

Leave a Reply