Perfectly understand closures in JavaScript

Viewed 16

I'm not really understanding the execution of the code below:

function outer() {
    function inner() {
        console.log('ok');
    }
    return inner;
}
let print_log = outer();
print_log();

I tried putting just outer() but it doesn't work, that is, just write:

function outer() {
    function inner() {
        console.log('ok');
    }
    return inner;
}
outer();

Why do I have to put outer() in a variable to get the console.log and then invoke it as a function?

0 Answers
Related