2017  Kodetalk | Feedback | Privacy Policy | Terms | About

Include a JavaScript file in another JavaScript file?

Is there anything like "import" in JavaScript that allows you to include a JavaScript file inside another JavaScript file?

First of all in JavaScript there is no import, include, or require. But there are other ways for JavaScript to include external JavaScript contents, though.

Here you can find the better solution achieving the functionality which you are looking

//its a synchronous Ajax request

function require(script) {
        url: script,
        dataType: "script",
        async: false,           // <-- This is the key
        success: function () {
            // all good...
        error: function () {
            throw new Error("Could not load script " + script);

And you can logically import by calling the above spinet require("/scripts/subscript.js");

And be able to call a function from the required script in the next line: subscript.doJavascriptFunctionCall();

Answer is