ES6: Use export to Reuse a Code Block

In the previous challenge, you learned about import and how it can be leveraged to import small amounts of code from large files. In order for this to work, though, we must utilize one of the statements that goes with import, known as export. When we want some code – a function, or a variable – to be usable in another file, we must export it in order to import it into another file. Like import, export is a non-browser feature.

The following is what we refer to as a named export. With this, we can import any code we export into another file with the import syntax you learned in the last lesson. Here’s an example:

const capitalizeString = (string) => {
return string.charAt(0).toUpperCase() + string.slice(1);
export { capitalizeString } //How to export functions.
export const foo = "bar"; //How to export variables.

Alternatively, if you would like to compact all your export statements into one line, you can take this approach:

const capitalizeString = (string) => {
return string.charAt(0).toUpperCase() + string.slice(1);
const foo = "bar";
export { capitalizeString, foo }

Either approach is perfectly acceptable.

Below are two variables that I want to make available for other files to use. Utilizing the first way I demonstrated export, export the two variables.



"use strict";
export const foo = "bar";
export const bar = "foo";

Definition – What does Web Programming mean?

Web programming refers to the writing, markup and coding involved in Web development, which includes Web content, Web client and server scripting and network security. The most common languages used for Web programming are XML, HTML, JavaScript, CSS and PHP. Web programming is different from just programming, which requires interdisciplinary knowledge on the application area, client and server scripting, and database technology.

At some point most web developers will be in the position of having to either provide hosting for their clients’ websites, or they will need to know which web hosts to recommend to their clients. Web hosting services store and activate all the files your website needs in order to function and to be active. Some of the best web hosting companies are: HostGator, GoDaddy, Bluehost, iPage, InMotion Hosting, HostFav, DreamHost, Interserver, Namecheap, 1 & 1, Fasthosts, Hostinger, GreenGeeks, Justhost, SiteGround, Host1Plus, eUKhost, Arvixe, FatCow, HostMonster and more.

Web Programming Degree

If you have a passion for connecting people through technology, our online Web Programming degree will provide you with in-demand skills to do so in a growing industry. In our program, you’ll become proficient in Java, Microsoft Azure, Google Developers Training, Web development and cloud computing—all of which help you prepare for various types of web programming roles, such as web developer, front-end developer, back-end developer or web app developer.

Through online Web Development courses, you’ll build your own professional portfolio for your future employers.


Leave a Reply

Your email address will not be published. Required fields are marked *



41 + = 48