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 my…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Sujith V S

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

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

home.js

import myAddress from './myAddress.js'
import addrs from './myAddress.js'

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"

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

home.js

import {myCountry as country, mySate} from './country.js'

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


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Sujith V S


Print Share Comment Cite Upload Translate Updates
APA

Sujith V S | Sciencx (2022-09-06T18:11:54+00:00) Import and Export in JavaScript.. Retrieved from https://www.scien.cx/2022/09/06/import-and-export-in-javascript/

MLA
" » Import and Export in JavaScript.." Sujith V S | Sciencx - Tuesday September 6, 2022, https://www.scien.cx/2022/09/06/import-and-export-in-javascript/
HARVARD
Sujith V S | Sciencx Tuesday September 6, 2022 » Import and Export in JavaScript.., viewed ,<https://www.scien.cx/2022/09/06/import-and-export-in-javascript/>
VANCOUVER
Sujith V S | Sciencx - » Import and Export in JavaScript.. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/09/06/import-and-export-in-javascript/
CHICAGO
" » Import and Export in JavaScript.." Sujith V S | Sciencx - Accessed . https://www.scien.cx/2022/09/06/import-and-export-in-javascript/
IEEE
" » Import and Export in JavaScript.." Sujith V S | Sciencx [Online]. Available: https://www.scien.cx/2022/09/06/import-and-export-in-javascript/. [Accessed: ]
rf:citation
» Import and Export in JavaScript. | Sujith V S | Sciencx | https://www.scien.cx/2022/09/06/import-and-export-in-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.