ES6 | Import and Export

The ES6 is a JavaScript standard. With the help of ES6, we can create modules in JavaScript. In a module, there can be classes, functions, variables, and objects as well. To make all these available in another file, we can use export and import. The export and import are the keywords used for exporting and importing one or more members in a module.

Export: You can export a variable using the export keyword in front of that variable declaration. You can also export a function and a class by doing the same.

  • Syntax for variable:
    export let variable_name;
  • Syntax for function:
    export function function_name() {
      // Statements
    }
    
  • Syntax for class:
    export class Class_Name {
      constructor() {
        // Statements
      }
    }
    
  • Example 1: Create a file named export.js and write the below code in that file.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    export let num_set = [1, 2, 3, 4, 5];
      
    export default function hello() {
        console.log("Hello World!");
    }
      
    export class Greeting {
        constructor(name) {
            this.greeting = "Hello, " + name;
        }
    }

    chevron_right

    
    

  • Example 2: In this example, we export by specifying the members of the module at the end of the file. We can also use alias while exporting using the as keyword.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    let num_set = [1, 2, 3, 4, 5];
      
    export default function hello() {
        console.log("Hello World!");
    }
      
    class Greeting {
        constructor(name) {
            this.greeting = "Hello, " + name;
        }
    }
      
    export { num_set, Greeting as Greet };

    chevron_right

    
    

  • Note: A default export should be specified here.

Import: You can import a variable using import keyword. You can specify one of all the members that you want to import from a JavaScript file.

  • Syntax:
    import member_to_import from “path_to_js_file”;
    // You can also use an alias while importing a member.
    import Greeting as Greet from "./export.js";
    // If you want to import all the members but don’t
    // want to Specify them all then you can do that using
    // a ' * ' star symbol.
    import * as exp from "./export.js";
  • Example 1: Create a file named import.html and write the below code in that file.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html lang="en">
        <head>
            <title>Import in ES6</title>
        </head>
        <body>
            <script type="module">
      
                // Default member first
                import hello, { num_set, Greeting } from "./export.js";
                console.log(num_set);
                hello();
                let g = new Greeting("Aakash");
                console.log(g.greeting);
            </script>
        </body>
    </html>

    chevron_right

    
    

  • Example 2:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html lang="en">
        <head>
            <title>Import in ES6</title>
        </head>
        <body>
            <script type="module">
                import * as exp from "./export.js";
      
                // Use dot notation to access members
                console.log(exp.num_set);
                exp.hello();
                let g = new exp.Greeting("Aakash");
                console.log(g.greeting);
            </script>
        </body>
    </html>

    chevron_right

    
    

  • Output: Output will be same, importing the same file.

Note: The default member should be imported first and then the none default members in curly brackets.

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.