Functional Programming (FP) and Object-Oriented Programming (OOP) are two of the most popular programming paradigms. Each has its strengths and trade-offs, depending on the problem you're solving. Let’s break it down!
🔹 Example:
jsCopyEditconst increment = (num) => num + 1; console.log(increment(5)); // 6
const increment = (num) => num + 1; console.log(increment(5)); // 6
jsCopyEditclass Counter { constructor() { this.value = 0; } increment() { this.value += 1; } } const counter = new Counter(); counter.increment(); console.log(counter.value); // 1
class Counter { constructor() { this.value = 0; } increment() { this.value += 1; } } const counter = new Counter(); counter.increment(); console.log(counter.value); // 1
jsCopyEditconst multiply = (factor) => (num) => num * factor; const double = multiply(2); console.log(double(5)); // 10
const multiply = (factor) => (num) => num * factor; const double = multiply(2); console.log(double(5)); // 10
jsCopyEditclass Animal { speak() { console.log("Animal sound"); } } class Dog extends Animal { speak() { console.log("Bark!"); } } const dog = new Dog(); dog.speak(); // "Bark!"
class Animal { speak() { console.log("Animal sound"); } } class Dog extends Animal { speak() { console.log("Bark!"); } } const dog = new Dog(); dog.speak(); // "Bark!"
.map()