Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Sichere ProgrammierungI audited my own ML linter and had to withdraw its best evidence(21.09.2026 um 22:54 Uhr)
Sichere ProgrammierungQuantum Result Validation for Distributed Computing Systems(21.09.2026 um 22:54 Uhr)
Sichere ProgrammierungJWT Authentication and Role-Based Access Control in LocalHands(21.09.2026 um 22:56 Uhr)
Sichere ProgrammierungStochastic Parrot or Alien Mind?(21.09.2026 um 22:56 Uhr)
Sichere ProgrammierungBuilding AI for the Physical World Is a Different Engineering Problem(21.09.2026 um 22:58 Uhr)
Sichere ProgrammierungI audited my own ML linter and had to withdraw its best evidence(21.09.2026 um 22:54 Uhr)
Sichere ProgrammierungQuantum Result Validation for Distributed Computing Systems(21.09.2026 um 22:54 Uhr)
Sichere ProgrammierungJWT Authentication and Role-Based Access Control in LocalHands(21.09.2026 um 22:56 Uhr)
Sichere ProgrammierungStochastic Parrot or Alien Mind?(21.09.2026 um 22:56 Uhr)
Sichere ProgrammierungBuilding AI for the Physical World Is a Different Engineering Problem(21.09.2026 um 22:58 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

JavaScript Modules 101: import & export Without Confusion

As JavaScript project grows managing code become more important than just writing it. Modules provide a simple way to organize, reuse and maintain code efficiently. In this blog we will cover : Why modules are needed Exporting…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!

As JavaScript project grows managing code become more important than just writing it.

Modules provide a simple way to organize, reuse and maintain code efficiently.



In this blog we will cover :




  • Why modules are needed

  • Exporting functions or values

  • Importing modules

  • Default vs named exports

  • Benefits of modular code







Modules



Before modules JavaScript don't have a proper way to organize code. Everything lived in global scope which often caused conflicts and messy code.



Developers used tricks like global variables or IFFE to manage this.





// Global variable (problem: can be overwritten)

var count = 0;
function increment() {
count++;
}








// IIFE (used to avoid global pollution)

(function(){
var count = 0

function increment(){
count++
console.log(count())

increment()
})()





But these were just workarounds, not a real solution.



Here comes the modules



A module is simply a separate piece of code with its own scope. It helps to keep the code organized , reusable and avoid conflicts that used to happen with global variable.



JavaScript supports various modules system. Here we only focus on the two most important types







Types of modules




  1. ES6 Modules



ES6 modules (ECMA Script 6) it is a native module in this you can group and only share what's needed using import and export keywords.



Exporting a Module



To create a module you define the code you want to expose using the export keyword.

You can export individually or entire module object




export const PI = 3.14
export function add(a , b){
return a + b
}






You can also export multiple things as a group:




const MULTIPLY = 'multiply';
const SUBTRACT = 'subtract';

export { MULTIPLY, SUBTRACT };






Importing a Module



To use the exported functionalities in another file, you use the import keyword. Here’s how you can import both named and default exports:



import { PI, add } from './math.js';

// Default export
import myFunction from './myFunction.js';






  1. CommonJS Modules



Before ES6 modules, JavaScript (especially in Node.js) used CommonJS to manage code between files. Instead of import and export, it used require to bring code in and module.exports to send code out.



Exporting with CommonJS



const PI = 3.14;
function add(a, b) {
return a + b;
}

module.exports = { PI, add };






Importing with CommonJS



const math = require('./math.js');
console.log(math.add(2, 3)); // Outputs: 5









Default Modules



Modern JavaScript lets you load files only when you actually need them using dynamic imports.



Instead of loading everything at once, you can load parts later making your app faster and lighter, especially in big projects.



// math.js (module)
export default function add(a, b) {
return a + b;
}







// app.js (using it)
import add from './math.js';

console.log(add(2, 3)); // 5









Default vs named exports






































Feature Named Export Default Export
Number of exports Multiple allowed Only one per file
Import syntax import { add } from './file' import add from './file'
Naming Must match exact name Can use any name
Curly braces Required {}
Not required
Use case Export multiple utilities/functions Export one main functionality






Benefits of modular code




  1. Better Organization



Modules keep your code clean and structured, so it’s easier to read and manage as your project grows.




  1. Reusability



You can reuse modules anywhere in your app (or even in other projects), saving time and effort.




  1. No Naming Conflicts



Each module has its own scope, so same variable names won’t clash with each other.




  1. Easy Testing



Smaller modules are easier to test individually, which makes your code more reliable.





Thanks for reading ! if enjoyed this blog , you can read more on this 👇


Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten JavaScript Modules 101: import & export Without Confusion

Thematisch verwandte Begriffe: JavaScript, Modules, import, export · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-79918 | MaxKB is an open-source AI assistant for enterprise. Prior to version 2.…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick