How do i import JavaScript file inside another JavaScript file ?
Question
Share
Sign Up to our social questions and Answers to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers to ask questions, answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Earlier javascript was not having any feature to import and export javascript but since the discovery of ECMAScript 6 (or ES6) you can use the export or import statement in a JavaScript file to export or import variables, functions, classes, or any other entity to/from other JS files.
Suppose we have two files named script.js and app.js and you want to export variables and functions script.js to app.js Then in the “script.js” file you need to write the export statement as shown in the following example:
Example :
let message = “Hi How are you doing?”;
const PI = 3.14;
function addNumbers(a, b){
return a + b;
}
// Exporting variables and functions
export { message, PI, addNumbers };
And in the “app.js” file you need to write the import statement as shown below:
import { message, PI, addNumbers } from ‘./script.js’;
console.log(message); // Prints to the console : Hi How are you doing?
console.log(PI); // Prints to the console : 3.14
console.log(addNumbers(10, 10)); // Prints to the console : 20