Why Do We Need Functions?


Suppose we want to add two numbers several times.

Without functions:



let result = 10 + 20;
console.log(result);

result = 30 + 40;
console.log(result);

result = 50 + 60;
console.log(result);






This approach leads to code duplication.

Using functions:



function add(i, j) {
let result = i + j;
...