Including a JavaScript file in another JavaScript file can be helpful in organizing your code, reusing common functions, and separating concerns. Fortunately, it’s easy to include a JS file in another JS file with the help of the script tag.
Here’s how to include a JS file in another JS file:
Create the JavaScript file you want to include: Let’s say you have a file called common.js, which contains functions that are commonly used in other JavaScript files.
Add the script tag to the file where you want to include the other JS file: If you want to include the common.js file in another file called main.js, you would add the following script tag to main.js:
<script src="common.js"></script>
Save the files: Make sure to save both files.
Test the files: Open the HTML file that references the main.js file in your web browser, and make sure that the functions in common.js are available to main.js.
That’s it! By including a JavaScript file in another JavaScript file, you can share common functions, organize your code, and separate concerns.