Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Sichere ProgrammierungThe Indie Dev Visibility Playbook: From Zero Users to Your First 100(21.09.2026 um 00:08 Uhr)
Sichere ProgrammierungBase, Chat and Reasoning Models: How Are They Different?(21.09.2026 um 00:09 Uhr)
Sichere ProgrammierungHexfield Deck is for Kanban lovers and Markdown believers(21.09.2026 um 00:20 Uhr)
Sichere ProgrammierungPermissions and Authorisation: A Practical Playbook(21.09.2026 um 00:21 Uhr)
Linux Tipps & Hardeningfilet | Terminal File Manager(20.09.2026 um 21:03 Uhr)
Linux Tipps & HardeningLooking for feedback on my Linux Distro(20.09.2026 um 21:03 Uhr)
Sichere ProgrammierungThe Indie Dev Visibility Playbook: From Zero Users to Your First 100(21.09.2026 um 00:08 Uhr)
Sichere ProgrammierungBase, Chat and Reasoning Models: How Are They Different?(21.09.2026 um 00:09 Uhr)
Sichere ProgrammierungHexfield Deck is for Kanban lovers and Markdown believers(21.09.2026 um 00:20 Uhr)
Sichere ProgrammierungPermissions and Authorisation: A Practical Playbook(21.09.2026 um 00:21 Uhr)
Linux Tipps & Hardeningfilet | Terminal File Manager(20.09.2026 um 21:03 Uhr)
Linux Tipps & HardeningLooking for feedback on my Linux Distro(20.09.2026 um 21:03 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

What is Functional Programming, and How Can You Do It in JavaScript?

Reagiere als Erste:r — dein Feedback zählt!

You: “Hey, I keep hearing about functional programming. It sounds cool, but what is it really, and how is it different from what I already do in JavaScript?”

Me: Great question! Let’s break it down step-by-step and compare functional programming (FP) with the traditional imperative way of coding.

🚀 What’s the Difference Between Functional and Imperative?

In imperative programming, you write step-by-step instructions on how to do something. It’s all about the sequence of tasks—updating variables, using loops, and modifying state directly.

In functional programming, you focus on what you want to do. You use pure functions, avoid direct mutations, and leverage declarative tools like map, filter, and reduce.

Let’s see this with side-by-side examples using a CRUD (Create, Read, Update, Delete) scenario for managing a list of users.

🛠️ Example 1: Adding a User

Imperative Way

Here’s an imperative example where we mutate the original array:

let users = [
  { id: 1, name: 'Alice' },
  { id: 2, name: 'Bob' }
];

function addUser(users, newUser) {
  users.push(newUser); // Directly modifies the array
}

addUser(users, { id: 3, name: 'Charlie' });
console.log(users);

Issues:

  1. Mutation: The push() method modifies the original array.
  2. Side Effects: If other parts of the code depend on users, they might be affected unexpectedly.

Functional Way

Here’s the functional approach, where we return a new array instead of mutating it:

const users = [
  { id: 1, name: 'Alice' },
  { id: 2, name: 'Bob' }
];

const addUser = (users, newUser) => [...users, newUser]; // Returns a new array

const newUsers = addUser(users, { id: 3, name: 'Charlie' });
console.log('Original:', users);
console.log('New:', newUsers);

Benefits:

  1. No Mutation: The original users array remains untouched.
  2. Predictable: Pure functions make it easy to understand the behavior.

🛠️ Example 2: Updating a User

Imperative Way

Here’s an example where we directly change an object inside the array:

let users = [
  { id: 1, name: 'Alice' },
  { id: 2, name: 'Bob' }
];

function updateUser(users, id, newName) {
  for (let i = 0; i < users.length; i++) {
    if (users[i].id === id) {
      users[i].name = newName; // Directly mutates the object
      break;
    }
  }
}

updateUser(users, 1, 'Alicia');
console.log(users);

Issues:

  1. Mutation: The object inside the array is directly updated.
  2. Verbosity: The for loop explicitly tells the computer how to update the user.

Functional Way

Here’s how we do it functionally using map and immutability:

const users = [
  { id: 1, name: 'Alice' },
  { id: 2, name: 'Bob' }
];

const updateUser = (users, id, newName) =>
  users.map(user =>
    user.id === id ? { ...user, name: newName } : user
  );

const updatedUsers = updateUser(users, 1, 'Alicia');
console.log('Original:', users);
console.log('Updated:', updatedUsers);

Benefits:

  1. No Mutation: We return a new array and leave the original untouched.
  2. Declarative: map focuses on what we want—transform the array—without managing loops explicitly.

🛠️ Example 3: Deleting a User

Imperative Way

Here’s the imperative version using a loop and direct modifications:

let users = [
  { id: 1, name: 'Alice' },
  { id: 2, name: 'Bob' }
];

function deleteUser(users, id) {
  for (let i = 0; i < users.length; i++) {
    if (users[i].id === id) {
      users.splice(i, 1); // Mutates the array
      break;
    }
  }
}

deleteUser(users, 2);
console.log(users);

Issues:

  1. Mutation: splice() modifies the original array.
  2. Complexity: Manual looping makes it harder to read.

Functional Way

Using filter, we can declaratively express the intention:

const users = [
  { id: 1, name: 'Alice' },
  { id: 2, name: 'Bob' }
];

const deleteUser = (users, id) => users.filter(user => user.id !== id);

const filteredUsers = deleteUser(users, 2);
console.log('Original:', users);
console.log('Filtered:', filteredUsers);

Benefits:

  1. No Mutation: filter creates a new array.
  2. Clean & Readable: The code clearly communicates the intent—keep users except the one with the given ID.

🎯 Summary Table: Functional vs Imperative

Aspect Imperative Functional
Data Mutation Mutates the original data (e.g., push, splice) Avoids mutation; creates new data structures
Readability Step-by-step, more verbose Declarative and concise
Side Effects More prone to unexpected side effects Pure functions eliminate side effects
Focus How to achieve a task (manual looping) What to achieve (use higher-order functions)
Tools Used Loops, conditionals map, filter, reduce, spread operator

🤔 Why Choose Functional Programming?

  • Your code becomes easier to reason about and test.
  • Avoiding side effects makes bugs less likely.
  • It’s more concise and declarative, reducing mental load.

You: “Got it! Functional programming focuses on writing predictable, clean code without mutating data. And it’s easier to read!”

Me: Exactly! By shifting from imperative to functional, you’ll unlock the real power of JavaScript. Start small—use map, filter, and reduce—and soon you’ll be coding functionally like a pro.

Ready to give it a try? Your future self will thank you! 🚀

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten What is Functional Programming, and How Can You Do It in JavaScript?

Thematisch verwandte Begriffe: What, Functional, Programming, JavaScript · 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-94084 | Suricata before 8.0.7 has an Http2ThreadMultiBuf use-after-free when a t…
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