Rich Business of import and export

Have you read my blogs on functions ? not yet try now .
because if you have read it or even know already how useful functions are ! then you must think about a situation .
you want to make an app for railway registrations . to enable ticket booking feature you must write a function . it is a tedious work :( . Now you got to know that someone has already written it and now you want that . Wouldn't that make your task a lot easier .
Fortunately , there is plenty of code out there in world written by someone else for everybody to use packed inside module .
How to import someone else code?
first you need to download the package/module via npm or any other package manager you like .
lets assume you are using npm .
there are two types of import statement require or import . require syntax is by default (type is commonjs) usable but if you want to use import syntax then in your package.json file add type:"module" .
like if you want to use express code , in commonjs
const express = require('express')
in module type
import express from "express"
How can you export you code ?
export is allowing other files or users to use the code you typed .
there are two types of export
name export
this way you can export multiple functions and variables .export const myFunction = () => {}; export class MyClass {} export let myVariable = 42;
or
{ myFunction, MyClass , myVariable }
default exports
this way you can export only one primary thing . by the name default it means you can import from that file using any name as only single thing is exported from the file.export default function myFunction() {}default export is only allowed once per file .
What is the primary service import/export gives?
these services help to make your code modular .
when working in a team , different functions can be written by team members in different files and to use them just export from the respective files and import in the file you want to use those functions.
in case of change you have to change at one location only that is a major advantage of modular code . you code in blocks and join these blocks into a full working system .






