Skip to main content Accessibility Feedback

ES Modules

export

Use the export operator to export modules for use in other files.

One way to do that is by adding the export operator before the variable or function expression.

export function add (num1, num2) {
	return num1 + num2;
}

export function subtract (num1, num2) {
	return num1 - num2;
}

Alternatively, you can include an export at the end as a single line, with an object of exported variables and functions. You can use the object property shorthand approach for this.

function add (num1, num2) {
	return num1 + num2;
}

function subtract (num1, num2) {
	return num1 - num2;
}

export {add, subtract};

Preorder my new course on Web Components! Want to learn how to build Web Components from scratch, master best practices, and more? Preorder today and get $100 off of the launch price.


Find this useful? You can support my work by purchasing an annual membership.