COURSES
MBAData Science & AnalyticsDoctorate Software & Tech AI | ML MarketingManagement
Professional Certificate Programme in HR Management and AnalyticsPost Graduate Certificate in Product ManagementExecutive Post Graduate Program in Healthcare ManagementExecutive PG Programme in Human Resource ManagementMBA in International Finance (integrated with ACCA, UK)Global Master Certificate in Integrated Supply Chain ManagementAdvanced General Management ProgramManagement EssentialsLeadership and Management in New Age BusinessProduct Management Online Certificate ProgramStrategic Human Resources Leadership Cornell Certificate ProgramHuman Resources Management Certificate Program for Indian ExecutivesGlobal Professional Certificate in Effective Leadership and ManagementCSM® Certification TrainingCSPO® Certification TrainingLeading SAFe® 5.1 Training (SAFe® Agilist Certification)SAFe® 5.1 POPM CertificationSAFe® 5.1 Scrum Master Certification (SSM)Implementing SAFe® 5.1 with SPC CertificationSAFe® 5 Release Train Engineer (RTE) CertificationPMP® Certification TrainingPRINCE2® Foundation and Practitioner Certification
Law
Job Linked
Bootcamps
Study Abroad
MS in Data AnalyticsMS in Project ManagementMS in Information TechnologyMasters Degree in Data Analytics and VisualizationMasters Degree in Artificial IntelligenceMBS in Entrepreneurship and MarketingMSc in Data AnalyticsMS in Data AnalyticsMS in Computer ScienceMaster of Science in Business AnalyticsMaster of Business Administration MS in Data ScienceMS in Information TechnologyMaster of Business AdministrationMS in Applied Data ScienceMaster of Business Administration | STEMMS in Data AnalyticsM.Sc. Data Science (60 ECTS)Master of Business AdministrationMS in Information Technology and Administrative Management MS in Computer Science Master of Business Administration Master of Business Administration-90 ECTSMSc International Business ManagementMS Data Science Master of Business Administration MSc Business Intelligence and Data ScienceMS Data Analytics MS in Management Information SystemsMSc International Business and ManagementMS Engineering ManagementMS in Machine Learning EngineeringMS in Engineering ManagementMSc Data EngineeringMSc Artificial Intelligence EngineeringMPS in InformaticsMPS in Applied Machine IntelligenceMS in Project ManagementMPS in AnalyticsMS in Project ManagementMS in Organizational LeadershipMPS in Analytics - NEU CanadaMBA with specializationMPS in Informatics - NEU Canada Master in Business AdministrationMS in Digital Marketing and MediaMSc Sustainable Tourism and Event ManagementMSc in Circular Economy and Sustainable InnovationMSc in Impact Finance and Fintech ManagementMS Computer ScienceMBA in Technology, Innovation and EntrepreneurshipMSc Data Science with Work PlacementMSc Global Business Management with Work Placement MBA with Work PlacementMS in Robotics and Autonomous SystemsMS in Civil EngineeringMS in Internet of ThingsMSc International Logistics and Supply Chain ManagementMBA- Business InformaticsMSc International ManagementMBA in Strategic Data Driven ManagementMSc Digital MarketingMBA Business and MarketingMSc in Sustainable Global Supply Chain ManagementMSc Digital Business Analytics MSc in International HospitalityMSc Luxury and Innovation ManagementMaster of Business Administration-International Business ManagementMS in Computer EngineeringMS in Industrial and Systems EngineeringMaster in ManagementMSc MarketingMSc Global Supply Chain ManagementMS in Information Systems and Technology with Business Intelligence and Analytics ConcentrationMSc Corporate FinanceMSc Data Analytics for BusinessMaster of Business AdministrationMaster of Business AdministrationMaster of Business AdministrationMSc in International FinanceMSc in International Management and Global LeadershipMaster of Business AdministrationBachelor of BusinessMaster of Business Administration 60 ECTSMaster of Business Administration 90 ECTSMaster of Business Administration 90 ECTSBachelor of Business AnalyticsBachelor of Information TechnologyMaster of Business AdministrationMBA Business AnalyticsMSc in Marketing Analytics and Data IntelligenceMS Biotechnology Management and EntrepreneurshipMSc in Luxury and Fashion ManagementMaster of Business Administration (90 ECTS)Bachelor of Business Administration (180 ECTS)B.Sc. Computer Science (180 ECTS) MSc in International Corporate Finance MSc in Sustainable Luxury and Creative IndustriesMSc Digital MarketingMSc Global Supply Chain Management (PGMP)MSc Marketing (PGMP)MSc Corporate Finance (PGMP)MSc Data Analytics for Business (PGMP)MS Business AnalyticsMaster of Business AdministrationMS Quantitative FinanceMS Fintech Management
For College Students

Explain module import and export in Javascript

$$/$$

In JavaScript, modules refers to independent and reusable code. Using modules in JavaScript, you can write code in one file and then share the same in another file so as to achieve code maintainability and reusability. Import and export keywords help you refer to modules and other code snippets written in another file.

 

As the names suggest, the export keyword is used to expose some piece of code written in a file and the import keyword is used to fetch some piece of code exported from another file. Now, there are two types of exports and imports - default export-import and named export-import.

 

Default export-import

The default export is written with the default keyword and there can be only one default export in a file. This means that only one code snippet can be exported as default from a file and thus, be imported in another file using default import. The default import should have the same name which is exported. Let’s take the help of an example to see how you write it. Suppose that you have a file named ‘utils.js’ where you have written a function for finding the sum of given numbers.

 

utils.js

const sum = (...arr) => {
  return arr.reduce((accumulator, currentValue) => accumulator + currentValue);
}

export default sum;

Alternatively, you can also write like the one given below where you can directly export the anonymous function sum by writing the export default keywords along with the function declaration:

 

utils.js

export default sum = (...arr) => {
  return arr.reduce((accumulator, currentValue) => accumulator + currentValue);
}

Now, in another file named ‘script.js’, you can access this function as:

 

script.js

import sum from 'utils.js';
console.log(sum(1, 2, 3, 4));

Named export-import

A named export can be used to expose more than code snippets written in a file and thus, all of them can be imported in some other file using named import. Note that the names of the code snippets to be exported-imported using named export-import are written within curly braces. This is how the default export is distinguished from named export. In default export, the default keyword is used whereas in named export, the names are written inside curly braces {}.

 

Consider that you have one more function which is used to calculate average of given numbers in the file named ‘utils.js’ and you want to export both the functions - sum as well as average.

 

utils.js

const sum = (...arr) => {
  return arr.reduce((accumulator, currentValue) => accumulator + currentValue);
}

const average = (...arr) => {
  return sum(...arr) / arr.length;
}

export {sum, average};

Now, in another file named ‘script.js’, you can access the sum function and average function individually as:

 

script.js

import {sum} from 'utils.js';
import {average} from 'utils.js';

console.log(sum(1, 2, 3, 4));
console.log(average(1, 2, 3, 4));

You can combine both the imports and thus write them in the same line as:

 

script.js

import {sum, average} from 'utils.js';

console.log(sum(1, 2, 3, 4));
console.log(average(1, 2, 3, 4));

You can optionally choose to give an alternative name to the named export using the ‘as’ keyword. Thus, you can write:

 

script.js

import {sum as add} from 'utils.js';

And then, you can access the sum function by the identifier add.

 

What if there are a lot of exported functions? Will you write all of them separating each other using comma? Well, you have got simpler method to reference all the named exports at once. You can use * followed by keyword ‘as’ followed by the alias name that you want to give to all the named exports. Thus, you can write:

 

script.js

import * as fn from 'utils.js';

console.log(fn.sum(1, 2, 3, 4));
console.log(fn.average(1, 2, 3, 4));

Here, ‘fn’ is the alias name given to all the functions exported from the ‘utils.js’ file using named exports.