How to Include a Javascript File in Another Javascript File?


You can include a Javascript file in another Javascript file by using the script tag. Here's an example:

// File 1: script1.js
function foo() {
  console.log('Hello, world!');
// File 2: script2.js
<script src="script1.js"></script>

foo(); // Call function from script1.js

In the example above, script2.js includes script1.js using the script tag. This allows foo() to be called in script2.js even though it's defined in script1.js. Note that this method only works if you're using the files in an HTML document, as the script tag is an HTML element.

Latest Questions

javascript How to Check if a Javascript Array Includes a Given Value? javascript How to Completely Uninstall Nodejs, and Reinstall From Scratch on Mac OS X? javascript How to Map Values of a Javascript Object to a New Object Using a Function?