why is the parameter passed to function B inside the function A when I called function A?

<script>
    window.something = (function(){
        return function(x){
        console.log(x);

        };  


    })();

    something("hello");
</script>

Just wondering, why is the parameter “hello” passed to the function inside the something function which has no parameter? Also, why doesn’t the something function execute immediately? Its a self-invoking function but its strange that I have to call it first before it can execute.



You are directly calling the function, also known as the module pattern.

So when having this code

something = (function(){
    return function(myparam) {
        // ...
    };  
})();

it is equivalent to this code:

something = function(myparam) {
    // ...
};

something is the result of an immediately-invoked function expression that returns the internal function. You then call that internal function with “hello”.

This is how closure works. You can a function and then again call the function with parameter for return function. Take a look at this example:-

Now , as you can see we stored the first function call in a variable called multi (which you can say is the function from the first return statement of the function multiply on line 2) and the using multi as a function we are passing it an argument for num2 .