ES6 in Depth
  • Introduction
  • Installation
  • Block Scope
    • Constant
    • Use cases
  • Template String
    • New String Methods
    • Tagged Template Literal
  • New Number Methods
  • Arrow Function
    • Default Parameters
    • Lexical This Scope
    • Where not to use arrow function
  • Object Enhancement
  • New Array Methods
    • For Of
  • Spread Operator
  • Destructuring
    • Array Destructuring
  • Class
  • Symbols
  • New Data Structures
    • Set
    • Map
    • WeakSet
    • WeakMap
    • Iterators
    • Generators
  • Promise
  • Import Export
Powered by GitBook
On this page
  • import/export multiple
  • export other variation
  • Rename exported function - addTwo as addTwoNumber
  • import all in one shot
  • Use case ( with lodash )
  • Default export/import

Was this helpful?

Import Export

  • named export/import

  • default export/import

// addition.js
function addTwo(a,b){
    return a + b;
}
export { addTwo };

// app.js
import { addTwo } from './addition';
console.log(addTwo(2, 4)); // 6

import/export multiple

// addition.js
function addTwo(a,b){
    return a + b;
}

function addThree(a,b,c){
    return a + b + c;
}

export { addTwo, addThree };

// app.js
import { addTwo, addThree } from './addition';

console.log(addTwo(2, 4)); // 6
console.log(addThree(2, 4, 5)); // 11

export other variation

// addition.js
export function addTwo(a,b){
    return a + b;
}

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

Rename exported function - addTwo as addTwoNumber

// addition.js
function addTwo(a,b){
    return a + b;
}

function addThree(a,b,c){
    return a + b + c;
}

export { addTwo, addThree };

// app.js
import { addTwo as addTwoNumber, addThree } from './addition';
console.log(addTwoNumber(6, 4)); // 10
console.log(addThree(2, 4, 5)); // 11

import all in one shot

// app.js
import * as adder from './addition';
console.log(adder.addTwoNumber(6, 4)); // 10
console.log(adder.addThree(2, 4, 5)); // 11

Use case ( with lodash )

// users.js
export var users = [
    {name: "hemant", age: 20, location: "hyderabad"},
    {name: "vinay", age: 80, location: "hyderabad"},
    {name: "paa", age: 90, location: "hyderabad"},
    {name: "varun", age: 44, location: "hyderabad"}
];

// app.js
import * as _ from "lodash"; // npm i lodash -S
import {users } from './users';

console.log(_.where(users, {age: 20}));

Default export/import

// greet.js
export default function greet(){

}


// app.js
import greet from './greet';
// OR
import myGreet from './greet'; // any name can be used for default export
greet();
PreviousPromise

Last updated 4 years ago

Was this helpful?