Javascript - Module Modules Exporting

Introduction

The 'export' keyword can be used to expose parts of code inside modules to other modules.

You can export a variable, a function, or a class declaration from a module.

Variables, Functions, or Classes not exported from a module are not accessible outside the module.


export var text = "message"; 
export let name = "Jack"; 
export const number = 7; 

Here, you are exporting text, name, and number, all declared using different variable declaration keywords.

You can also export a function or a Class from the module.

export function add(a, b) { 
    return a + b; 
} 

export class Rectangle { 
    constructor(length, width) { 
        this.length = length; 
        this.width = width; 
    } 
} 

Here, we are exporting the function add and Class Rectangle.

You can export an existing function that is private to the module as well.

function multiply(a, b) { 
    return a * b; 
} 
export { multiply }; 

Related Topics

Quiz