DEV Community

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

Posted on • Edited on

1 1

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.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay