ES6 Import and Export Last Updated : 03 Jan, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report 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. javascript 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; } } 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. javascript 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 }; 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. html <!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> Example 2: html <!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> 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. Comment More infoAdvertise with us Next Article React Importing and Exporting Components A aakashpawar1999 Follow Improve Article Tags : JavaScript Web Technologies ES6 Similar Reads Import and Export in Node.js Importing and exporting files are important parts of any programming language. Importing functions or modules enhances the reusability of code. When the application grows in size, maintaining a single file with all the functions and logic becomes difficult. It also hinders the process of debugging. 3 min read React Importing and Exporting Components In React, components are the building blocks of any application. They allow developers to break down complex user interfaces into smaller, reusable pieces. To manage these components efficiently, you need to understand how to import and export them.In this article, we will explore the different meth 6 min read How to Import and Export SQL Server Database? Creating and managing a SQL Server database is an essential skill for database administrators and developers. In this article, We will go through the process of setting up a database in SQL Server, from creating the database and tables to inserting records, and finally, exporting and importing the d 3 min read export command in Linux with Examples The 'export' command is one of the essential built-in commands in the Bash shell, allowing users to manage environment variables effectively. It is defined in POSIX standards, which state that the shell will assign the export attribute to specified variables, causing them to be included in the envir 2 min read How To Use An ES6 Import In Node.js? To use an ES6 import in Node.js, you need to make some configurations to support ES6 modules. While Node.js was originally built around the CommonJS module system (require() and module.exports), modern JavaScript development prefers using the ES6 module syntax (import and export). Node.js now suppor 4 min read Difference Between Node Require and ES6 Import And Export Importing modules makes your code reusable. NodeJS offers two ways to do this: CommonJS (require()) and ES6 modules (import/export). Both achieve the same goalâsharing code, but use different syntax.CommonJS is the older, traditional way. ES6 modules are newer and offer some advantages, but both are 5 min read Like