Import & Export Syntax


Exporting and importing Example one

Exporting - external.js


//Method one - Exporting
//Exporting values and functions by adding the export keyword before
//export let keyValue = 1000;
//export let test = () => console.log("test function")

//console.log(keyValue);

//Method two - Exporting
//Alternatively you can export as follows

let keyValue = 1000;
let test = () => console.log("test function")

console.log(keyValue);

export {keyValue, test};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

Importing - app.js


'use strict';

// Write ES6 code here...

//Importing Values
import { keyValue, test } from './external.js';


test();

1
2
3
4
5
6
7
8
9
10
11

Exporting and importing Default Values

  • you can only export one default value
  • you can use aliases

Exporting - external.js

//ES6 Modules and classes - Additional Syntax

let keyValue = 1000;
let test = () => console.log("test function")

let ab = 'Some Default Value';


export {keyValue, test};

//The 'Default' Keyword
export default ab;

1
2
3
4
5
6
7
8
9
10
11
12
13

Importing - app.js

'use strict';

// Write ES6 code here...

//Importing Default values
//import ab from './external.js'
//Importing Values
///import { keyValue, test } from './external.js';

//Importing as aliases
//import { keyValue as ValueTwo, test } from './external.js';

//Importing default and non default
//import ab, { keyValue, test } from './external.js';

//Importing All
import * as imported from 'external.js';

console.log(imported.ab);
console.log(imported.keyValue);
imported.test();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Last Updated: 8/13/2019, 6:55:26 PM