Learn
Intermediate JavaScript Modules
import

ES6 module syntax also introduces the import keyword for importing objects. In our order.js example, we import an object like this:

import Menu from './menu';
  1. The import keyword begins the statement.
  2. The keyword Menu here specifies the name of the variable to store the default export in.
  3. from specifies where to load the module from.
  4. './menu' is the name of the module to load. When dealing with local files, it specifically refers to the name of the file without the extension of the file.

We can then continue using the Menu module in the order.js file.

Instructions

1.

In missionControl.js we’ll use the module Airplane to display the fuel capacity of both our airplanes.

Use the import keyword to import the Airplane module.

2.

Define a function displayFuelCapacity().

3.

Within the body of the displayFuelCapacity function, use forEach() to iterate over the Airplane.availableAirplanes array.

The forEach() should take an anonymous function as a parameter. We’ll add more in the next step.

4.

Pass the anonymous function you created in the last step a parameter of element.

5.

Within the displayFuelCapacity function, use console.log() to output the element’s name and its fuel capacity. The output should look like this:

'Fuel Capacity of + (element name) : + (element fuelCapacity)'
6.

Call the displayFuelCapacity function.

Folder Icon

Take this course for free

Already have an account?