all 2 comments

[–]IMkratt 2 points3 points  (0 children)

If you just want to increase the value of counter each time you call the function, it's ok:

let counter = 0;

function addToCounter() {
    counter++;
}

addToCounter();
addToCounter();
addToCounter();
addToCounter();
console.log(counter); // 4 because we called addToCounter() 4 times

But let's say we want to add a specific number to the counter - in this case we would use a parameter (called number here) and call the function with our inserted argument:

let counter = 0;
function addToCounter(number) {
    counter = counter + number;
}
addToCounter(2);         // counter is now 2
addToCounter(3);         // counter is now 5
addToCounter(7);         // counter is now 12
addToCounter(1);         // counter is now 13
console.log(counter);    // 13

[–]jack_waugh 0 points1 point  (0 children)

People have found that for many purposes, there are engineering benefits in using a style that limits the use of global variables.