How to include a JavaScript file in another JavaScript file

0 votes

How to include a JavaScript file in another JavaScript file?

I'm working on a project where I need to modularize my code by splitting it into multiple JavaScript files for better organization. I want to know how I can include a JavaScript file inside another JavaScript file so that I can use the functions and variables defined in one file within another. What’s the best way to achieve this?

Nov 27, 2024 in Java-Script by Nidhi
• 5,440 points
65 views

1 answer to this question.

0 votes

Let's assume you have two files:

  1. math.js: This file contains a simple function to add two numbers.

  2. app.js: This file will import the math.js functions and use them.

1. Exporting from the first file (math.js)

In the file where you define your functions or variables (math.js in this case), you need to export them using module.exports.

math.js:

// math.js

// Define a function

function add(a, b) {

    return a + b;

}

// Export the function so it can be used in other files

module.exports = {

    add

};

Here, we are exporting the add function from math.js so that other files can import and use it.

2. Importing in the second file (app.js)

In the file where you want to use the functions from another file (app.js in this case), you import the functions using the require() function.

app.js:

// app.js

// Import the `add` function from the `math.js` file

const math = require('./math');

// Use the imported function

const result = math.add(3, 5);

console.log(`The sum is: ${result}`);

In this file:

  • require('./math') imports the module (i.e., math.js), and the result is assigned to the math variable.

  • We then access the add function from the math module using math.add().

answered Nov 27, 2024 by kavya

Related Questions In Java-Script

0 votes
1 answer

How do I include a JavaScript file in another JavaScript file?

Hello @kartik, It is possible to dynamically generate ...READ MORE

answered Jul 27, 2020 in Java-Script by Niroj
• 82,840 points
893 views
0 votes
1 answer

How to access PHP session variables from jQuery function in a .js file?

Hello, You can produce the javascript file via ...READ MORE

answered Apr 29, 2020 in Java-Script by Niroj
• 82,840 points
13,129 views
0 votes
1 answer

How to include csrf_token() in an external js file in Laravel?

Hello @kartik, To resolve this error you can ...READ MORE

answered Jun 11, 2020 in Java-Script by Niroj
• 82,840 points
3,961 views
0 votes
1 answer

How to parse Excel file in Javascript/HTML5?

Hello @kartik, Below Function converts the Excel sheet ...READ MORE

answered Jun 19, 2020 in Java-Script by Niroj
• 82,840 points
14,480 views
0 votes
1 answer

Presenting docket dtates inside html page by javascript

Use the Docker Engine Api:Docker Engine API ...READ MORE

answered Jun 20, 2018 in Docker by DareDev
• 6,890 points
790 views
0 votes
1 answer

Migrating proxy npm repo in nexus 3

I don't think you can achieve this ...READ MORE

answered Jun 22, 2018 in DevOps Tools by DareDev
• 6,890 points
1,565 views
+1 vote
1 answer

What is the difference between JavaScript and Java

This quote rightly explains that 2 totally ...READ MORE

answered Jun 29, 2018 in Java by Daisy
• 8,140 points
820 views
0 votes
1 answer

How can I replace every instance of a string in JavaScript?

In JavaScript, replacing all instances of a ...READ MORE

answered Jan 10 in Java-Script by Navya
41 views
0 votes
1 answer

What are the methods to clear all elements in a JavaScript array?

In JavaScript, there are several methods to ...READ MORE

answered Jan 10 in Java-Script by Navya
47 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP