Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 10

What is Module

In JavaScript, Module is a JavaScript file where we


write codes. The object in a module are not available for
use, unless the module file exports them.
Exporting Module
export - The export statement is used when creating JavaScript modules to
export functions, objects, or primitive values from the module so they can be
used by other programs with the import statement.
There are two different types of export - named and default. You can have
multiple named exports per module but only one default export.
Default Export
You can have only one default export per module. A default export can be
imported with any name.
mobile.js
export default class Nokia { export default function show() {
Properties
Methods } export default const a = 10;
}
class Nokia { function show() { const a = 10;
Properties } export default a;
Methods export default show;
}
export default Nokia;
Named Export
You can have multiple named exports per module. Named exports are useful to export several
values. During the import, it is mandatory to use the same name of the corresponding object.

mobile.js
export class Nokia { export function show () { export const a = 10;
Properties
Methods }
}

class Nokia { function show () { const a = 10;


Properties export {a};
Methods }
}
export {show};
export {Nokia};
Named Export
You can have multiple named exports per module. Named exports are useful to export several
values. During the import, it is mandatory to use the same name of the corresponding object.

mobile.js
class Nokia {
Properties
Methods
}
export {Nokia, show};
function show () {

}
export const a = 10;
Importing Module
import - The static import statement is used to import bindings which are
exported by another module. Imported modules are in strict mode whether you
declare them as such or not.
Importing Defaults
You can have only one default export per module. A default export can be
imported with any name.

mobile.js app.js
class Nokia { import Nokia from ‘./mobile.js’
Properties
Methods
}
export default Nokia;
Importing Named
You can have multiple named exports per module. Named exports are useful
to export several values. During the import, it is mandatory to use the same
name of the corresponding object.
mobile.js app.js
export class Nokia {
import {Nokia} from ‘./mobile.js’
Properties
Methods import {Nokia, show}from ‘./mobile.js’
}
function show () {

}
export {show};
Importing All
mobile.js app.js
class Nokia { import * as device from ‘./mobile.js’
Properties
Methods
} device.Nokia
export function show () { device.show
device.a
}
Export const a = 10;
export {Nokia};
Importing Default and Named
mobile.js app.js
class Nokia { import Nokia, {show} from ‘./mobile.js’
Properties
Methods
}
export function show () {

}
export default Nokia;

You might also like