DEV Community

Discussion on: Understanding callback functions and closures in JavaScript.

Collapse
 
colocodes profile image
Damian Demasi

Nice article! I found a couple of mistakes, though.

Regarding HOF:
In the first code snippet, the printMsg function is missing an argument. It could be called like so:

//...
printMsg(isEven, 2);
Enter fullscreen mode Exit fullscreen mode

Regarding callbacks:
The code is not correct. It should be:

let numbers = [1, 2, 4, 7, 3, 5, 6];

function isOddNumber(number) {
  return number % 2 !== 0;
}

const oddNumbers = numbers.filter(isOddNumber);
console.log(oddNumbers); // [1, 7, 3, 5]
Enter fullscreen mode Exit fullscreen mode

Regarding closures and exceptions:
The definition is on point, awesome! But with the exception, the code has a mistake: the new Function statement is missing an argument:

//...
let func = new Function('value', 'alert(value)');
//...
Enter fullscreen mode Exit fullscreen mode

In this way, the actual result is undefined.

Collapse
 
swastikyadav profile image
Swastik Yadav

I really appreciate this, Damian. Thank you very much for correcting my mistakes and taking the time to write this. 😊

I have corrected the code snippets.