Introduction
Suppose you wrote some functions in a Javascript program. Now you want to use these functions in another JavaScript program. Instead of writing the functions again in the new script, you can import the functions from your previously written program into our new script.
JavaScript supports importing of methods from one program to another.
Let’s explore modules in JavaScript with a working example:
In this example, we’ll first create a canvas on our HTML page. Then on that canvas, we’ll draw two squares or rectangles. One square will be of fixed shape, size, color, and we’ll fix its position on the canvas, whereas the next square will be of a random shape, size, and color, the position of the second square will also be random on the canvas.
Also Read, Javascript hasOwnProperty
Working of the program
- Our main working directory will have one HTML file - ‘index.html,’ one javascript file - ‘index.js’ and a folder named ‘modules.’
- The modules folder will contain two javascript files, ‘canvas.js’ and ‘square.js.’
Caption: Directory Structure
- First of all, in our HTML file, we’ll write some basic boilerplate code and include the index.js as module type in the script tag.
Also, we’ll style the canvas element with a black border.
index.html
You can also try this code with Online Javascript Compiler
|
-
Now, in our modules folder in the square.js file, we’ll create some functions.
- draw() - This function will draw a square on the canvas with a given length, color, and x-y coordinates.
- random() - This function will give us a random number from the provided range of numbers.
- reportArea() - A function to calculate the area of the square.
- reportPerimeter() - A function to calculate the perimeter of the square.
- randomSquare() - This function will generate a square with a random color, random x-y coordinates, and random length.
After creating these functions, we’ll have to export these by giving the export command (check syntax below) to use in some other program.
/modules/square.js
You can also try this code with Online Javascript Compiler
|
- The canvas.js file will also contain some functions that will be used to create canvas elements and manage the reportList.
(We will be displaying the area and perimeter of a square below the canvas, so here the reportList will contain the text elements to show perimeter and area)
The functions inside this file are:
- create() - This function will create a canvas of a given height and width. After creating it will append this to an HTML parent element with the given id.
- createReportList() - This function will create the list used to display the area and perimeter of the square (like we discussed above).
Here also, we’ll need to export these functions to use them outside this program.
/modules/canvas.js
You can also try this code with Online Javascript Compiler |
- Finally, coming on to our main JavaScript file, index.js.
At the beginning of our file, we’ll first import the required functions from their respective locations.
For example, the line - import { create, createReportList } from './modules/canvas.js'; imports the create() and createReportList() method from the canvas.js file.
‘./’ this specifies the current working directory, hence ‘./modules/canvas.js’ means that we’ll search for the modules folder in our current directory and then search for canvas.js in the modules folder.
After importing the necessary methods, we’ll create a canvas and a reportList using the methods we imported earlier and create two squares (one fixed and one random).
index.js
You can also try this code with Online Javascript Compiler
|
Output

Caption: Displayed HTML page
Caption: Displayed HTML page
As you can see on the web pages above, one square is fixed, and the other will keep changing its shape, size, color, and position with each reload.
You can practice it on an online javascript compiler.