Sunday, December 29, 2024
Google search engine
HomeLanguagesJavascriptHow to export multiple values or elements in a Module ?

How to export multiple values or elements in a Module ?

Modules in JavaScript contain some data or functions which can be reused anywhere by calling them. The export statement is used while creating these modules so that, the same modules can be imported in any other part of code to use those data and perform some tasks(carried out by functions) repetitively.

Syntax: The following syntax is used to export the multiple values or elements from the module:

export let element1
export const someValue
export function foo(){...}

Note: As we are exporting multiple values, while importing those values it is mandatory to use the same name of the corresponding object

Project Structure:

Project Structure of Code

Here, In the root folder of “GFG-MODULES” there are 3 files namely “index.html”,  “index.js” and our “package.json” file along with these, it has the “modules” folder containing a file named “siteData.js”.

Example:

Step 1: Configuring package.json file so that it won’t cause any errors when export statements are used

Firstly in our package.json file, add the following property:

"type" : "module"

When you have the “type: module” property, then your source code can use the import syntax, otherwise, it will cause errors and will only support the “require” syntax. Your package.json file should look similar to this

Javascript




{
  "name": "gfg-modules",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "GFG",
  "license": "ISC"
}


Step 2: Exporting multiple elements from the module. In siteData.js, we have exported multiple elements from our module “siteName”.

Javascript




export const siteName = "GeeksForGeeks";
export const url = "https://www.geeksforgeeks.org/";
export const founderName = "Sandeep Jain";
export const aboutSite = "A Computer Science portal for neveropen";
export let siteContent =
  "Computer science and Programming articles along with Courses";


Step 3: Importing the multiple elements in index.js:

index.js




import {
  siteName,
  url,
  founderName,
  aboutSite,
  siteContent,
} from "./modules/siteData.js";
  
console.log("Site Data is as follows:");
console.log(`Site name: \t${siteName}`);
console.log(`Site url : \t${url}`);
console.log(`Founder name: \t${founderName}`);
console.log(`About site: \t${aboutSite}`);
console.log(`Site Content: \t${siteContent}`);


Here, as we have exported multiple values, hence our export type was “Named Exports” and due to it, we have imported all the values by the same corresponding names.

Step to run the application: Open the terminal and type the following command.

node index.js

Output:

Site Data is as follows:
Site name:      GeeksForGeeks
Site url :      https://www.geeksforgeeks.org/
Founder name:   Sandeep Jain
About site:     A Computer Science portal for neveropen
Site Content:   Computer science and Programming articles along with Courses

Thus, we have seen how to export multiple elements from a module.

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments