DEV Community

Cover image for Import and Export in JavaScript.
Sujith V S
Sujith V S

Posted on • Updated on

Import and Export in JavaScript.

There are two types of exports in javascript.
1) default export
2) named export

Default Export
Let's create a file named myAddress.js

myAddress.js

const myAddress = {
    name:"Sujith",
    country:"India",
    phone:45342....,
}
export default myAddress
Enter fullscreen mode Exit fullscreen mode

Now in home.js, let's import the object in myAddress.js

home.js

import myAddress from './myAddress.js'
import addrs from './myAddress.js'
Enter fullscreen mode Exit fullscreen mode

export default myAddress always defaultly exports the myAddress object. So even if we import addrs from myAddress.js, we only get myAddress object because it is defaultly exported.

Named Export
Let's create a file named myAddress.js

country.js

export const myCountry = () => {console.log(India)}
export const myState = "Kerala"
Enter fullscreen mode Exit fullscreen mode

Now in home.js, let's import the object in myAddress.js

home.js

import {myCountry as country, mySate} from './country.js'
Enter fullscreen mode Exit fullscreen mode

In country.js we are exporting each function, objects, variables individually. So we can import each one of them individually.

Top comments (0)