Import Export
// 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}));
Last updated
Was this helpful?