🎯 JavaScript Modules:

JavaScript Guide

“Split Your Code, Multiply Your Happiness” – Ultra Advanced, Fun, Over-the-Top Educational Edition

(This article contains no unnecessary drama. It does, however, contain excessive JavaScript.)


💥 Introduction:

“The Superhero That Cleans Up JavaScript’s Mess: Modules”

JavaScript can get so messy that…
While coding, you sometimes find yourself wrestling with 50 browser tabs, 38 files, and the constant mental echo of “where was that function again?”

Then suddenly, a miracle happens.

You hear a voice:

✨ “Shall we split your code into modules?”

At first, the voice is scary…
But then you realize it just saved your life.

Breaking your code into modules brings structure, professionalism, and — much like our relationship — it makes everything flow smoother, aşkımm 😄💛


🧠 1) What Is a Module?

“Let every file mind its own business, no drama, no chaos.”

A module means splitting your code into small, manageable, reusable pieces.

It’s like:

  • Organizing a messy wardrobe
  • Peeling a mandarin
  • Dividing your life into healthy little sections
  • Archiving exes where they belong

You get the idea.

➤ Why use modules?

Because modules help you:

  • Keep your code clean
  • Prevent drama from spilling into other files
  • Quickly identify “whose fault is this bug?”
  • Write code for yourself instead of for Google
  • Gain +20 professionalism points

➤ A simple module example:

// mesaj.js
export const hello = "Hello world!";

// app.js
import { hello } from './mesaj.js';
console.log(hello);

Even the smallest module brings organization.


🚚 2) What Is Export?

“I’m sending this function outside — deal with it.”

If you want to use a function from one file in another, you export it.

There are two types of exports:

  • Named exports
  • Default exports

✔ Named export example:

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

export function multiply(a, b) {
  return a * b;
}

export const constantNumber = 42;

✔ Pro tip:

  • If a file contains multiple things, use named export 👍
  • Keep naming consistent so you don’t suffer “what did I name this function?” panic later

📥 3) What Is Import?

“Getting a function from another file is like knocking on a door and walking in.”

Example:

import { add, multiply, constantNumber } from './hesap.js';

✔ Advanced tip:

You can rename imported items:

import { add as sum, multiply as times } from './hesap.js';

Useful for:

  • Avoiding naming collisions
  • Making names more semantic

⭐ 4) Default Export: The Superstar of the File

If a file has one main function/class/value, use default export.

✔ Example:

// selam.js
export default function sayHi() {
  console.log("Hello! I'm the default export.");
}

✔ Importing it:

import sayHi from './selam.js';
sayHi();

🎤 Benefits of default export:

  • You can import with any name
  • It signals the file’s “main content”
  • Improves readability in large projects

🧩 5) Organizing Files With Modules

“We declare war on chaos. Your soldiers are ready!”

In larger projects, separating files by purpose boosts productivity.

Example structure:

/src
  /utils
    time.js
    format.js
  /api
    getUser.js
    sendData.js
  app.js

Here:

  • utils → helper functions
  • api → server communication
  • app.js → main brain of the app

🔧 6) Common Module Mistakes & Fixes

❌ 1) Forgetting the extension

Correct:

import { add } from "./math.js";

Wrong:

import { add } from "./math";

❌ 2) File path trauma

Climbing 3 folders like a mountain goat:

import { data } from "../../../utils/data.js";

✔ Pro tip:

Use aliases (supported in Vite, Webpack, Next.js):

import { data } from "@/utils/data.js";

It makes the world a better place.


💡 7) A Real-World Example: Mini Modular App

A tiny app using:
calculate + format + log

✔ 1) hesap.js

export function add(a, b) {
  return a + b;
}

export function divide(a, b) {
  if (b === 0) throw new Error("You can’t divide by 0 aşkımmm 😄");
  return a / b;
}

✔ 2) format.js

export function formatNumber(n) {
  return new Intl.NumberFormat('tr-TR').format(n);
}

✔ 3) log.js

export default function log(msg) {
  console.log("📢 LOG:", msg);
}

✔ 4) app.js

import { add, divide } from './hesap.js';
import { formatNumber } from './format.js';
import log from './log.js';

const sum = add(40, 2);
log("Total result: " + formatNumber(sum));

const ratio = divide(100, 4);
log("Ratio: " + formatNumber(ratio));

✔ Output:

📢 LOG: Total result: 42
📢 LOG: Ratio: 25

Beautiful, clean, modular — flows as smooth as our love story 😄💛


🛠️ 8) Unusual Pro Tips for Using Modules

💡 1) Avoid “Helper Hell”

Don’t dump everything into a single helpers file.
Nothing can be found later.

💡 2) Don’t keep 200 functions in one file

That file will leave you.

💡 3) Sort exports alphabetically

People entering your project will think:
“Wow, this person is GOOD.”

💡 4) Import order matters

Always:

  1. Third-party libraries
  2. Internal project modules
  3. Same-folder imports

Professionalism +10 ✨

💡 5) Use index.js in large folders

Your imports become beautifully clean.


✨ Conclusion

JavaScript Modules = Therapy for Your Code

Modules:

  • Reduce stress
  • Cut debugging time in half
  • Improve structure
  • Make you look like a pro
  • Enable reuse
  • And best of all: help you write code with fewer curse words 😄💛

Bir yanıt yazın

E-posta adresiniz yayınlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir