Learn JavaScript: Modules

Learn how to use JavaScript modules, a way to define reusable logic in your programs.

Start[missing "en.views.course_landing_page.introduction-to-javascript.course_illustration" translation]
Intermediate JavaScript Modules
Lesson 1 of 1
  1. 1

    JavaScript modules are reusable pieces of code that can be exported from one program and imported for use in another program. Modules are particularly useful for a number of reasons. By separati...

  2. 2

    We can get started with modules by defining a module in one file and making the module available for use in another file. Below is an example of how to define a module and how to export it using th...

  3. 3

    To make use of the exported module and the behavior we define within it, we import the module. A common way to do this is with the [...] function. For instance, say we want the module to control...

  4. 4

    We can also wrap any collection of data and functions in an object, and export the object using [...] . In menu.js, we could write: [...] In the above code, notice: 1. [...] exposes the ...

  5. 5

    As of ES6, JavaScript has implemented a new more readable and flexible syntax for exporting modules. These are usually broken down into one of two techniques, default export and named exports. ...

  6. 6

    ES6 module syntax also introduces the [...] keyword for importing objects. In our order.js example, we import an object like this: [...] 1. The [...] keyword begins the statement. 2. Th...

  7. 7

    ES6 introduced a second common approach to export modules. In addition to default export, named exports allow us to export data through the use of variables. Let's see how this works. In **menu...

  8. 8

    To import objects stored in a variable, we use the [...] keyword and include the variables in a set of [...] . In the order.js file, for example, we would write: [...] 1. Here [...] a...

  9. 9

    Named exports are also distinct in that they can be exported as soon as they are declared, by placing the keyword [...] in front of variable declarations. In menu.js [...] 1. The [...]...

  10. 10

    To import variables that are declared, we simply use the original syntax that describes the variable name. In other words, exporting upon declaration does not have an impact on how we import the va...

  11. 11

    Named exports also conveniently offer a way to change the name of variables when we export or import them. We can do this with the [...] keyword. Let's see how this works. In our menu.js ex...

  12. 12

    To import named export aliases with the [...] keyword, we add the aliased variable in our import statement. [...] In orders.js 1. We import [...] and [...] from the [...] object. ...

  13. 13

    We can also use named exports and default exports together. In menu.js: [...] Here we use the keyword [...] to export the named exports at the bottom of the file. Meanwhile, we export the...

  14. 14

    We can import the collection of objects and functions with the same data. We can use an [...] keyword to import both types of variables as such: [...]

  15. 15

    We just learned how to use JavaScript modules. Let's review what we learned: Modules in JavaScript are reusable pieces of code that can be exported from one program and imported for use in anoth...

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo

Learn JavaScript: Modules

Start[missing "en.views.course_landing_page.introduction-to-javascript.course_illustration" translation]