2018-09-30 23:01:58 +01:00
---
id: 587d7b8c367417b2b2512b56
title: Use export to Reuse a Code Block
challengeType: 1
---
## Description
< section id = 'description' >
2019-05-30 09:04:11 -05:00
Imagine you have a function, < code > capitalizeFirstLetter< / code > , that simply takes in a string and returns the string with the first letter capitalized. You want to use this function in several different javascript files. In order to share the function with the files, you need to first < code > export< / code > it.
2019-05-17 06:20:30 -07:00
```js
2019-05-17 06:33:17 -05:00
export const capitalizeFirstLetter = (string) => {
2019-05-17 06:20:30 -07:00
return string.charAt(0).toUpperCase() + string.slice(1);
}
```
2019-05-17 06:33:17 -05:00
The above is a common way to export a single function, but you can achieve the same thing like this:
2019-05-17 06:20:30 -07:00
```js
2019-05-17 06:33:17 -05:00
const capitalizeFirstLetter = (string) => {
2019-05-17 06:20:30 -07:00
return string.charAt(0).toUpperCase() + string.slice(1);
}
2019-05-17 06:33:17 -05:00
export { capitalizeFirstLetter };
2019-05-17 06:20:30 -07:00
```
2019-05-30 09:04:11 -05:00
After you export a function like this, you can import it in another file to use without having to rewrite the function.
2018-09-30 23:01:58 +01:00
< / section >
## Instructions
< section id = 'instructions' >
2019-05-17 06:33:17 -05:00
Create and export a variable named < / code > < / code >
2018-09-30 23:01:58 +01:00
< / section >
## Tests
< section id = 'tests' >
```yml
2018-10-04 14:37:37 +01:00
tests:
- text: < code > foo</ code > is exported.
2018-10-20 21:02:47 +03:00
testString: getUserInput => assert(getUserInput('index').match(/export\s+const\s+foo\s*=\s*"bar"/g), '< code > foo</ code > is exported.');
2018-10-04 14:37:37 +01:00
- text: < code > bar</ code > is exported.
2018-10-20 21:02:47 +03:00
testString: getUserInput => assert(getUserInput('index').match(/export\s+const\s+bar\s*=\s*"foo"/g), '< code > bar</ code > is exported.');
2018-09-30 23:01:58 +01:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
"use strict";
2019-05-30 09:04:11 -05:00
2018-09-30 23:01:58 +01:00
```
< / div >
### Before Test
< div id = 'js-setup' >
```js
2019-01-15 18:29:33 +03:00
self.exports = function(){};
2018-09-30 23:01:58 +01:00
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
2018-12-15 07:11:12 +00:00
"use strict";
export const foo = "bar";
export const bar = "foo";
2018-09-30 23:01:58 +01:00
```
< / section >