"Uncaught TypeError: undefined is not a function" in JavaScript code block

Missed a ; after declaring b. The following code is equivalent to what you have.

var b = function() {
   console.log ('outer world');
}(function() {})();

Without the ; b becomes self-executing and takes an empty function as a parameter. After that it self-executes again; however, because b does not return a function, you get an exception.

I suggest not to skip ; until you become js ninja :). Keep b inside to avoid global scope pollution.

(function () {
    "use strict";

    var b = function () {
        console.log("hi");
    };
    // other code
}());

Semicolon auto-insertion discussion

In case you do not want semicolons, add an operator before self-running function

var b = function () { console.log('outer world') }
;(function() { /* ... */ }())

ES6 Update:

(() => {
  const b = () => console.log('hi')
  // ...
})()

You need a semicolon after your var declaration, and by the way, please remove the space between log and (

var b = function() {
       console.log('outer world');
};

Tags:

Javascript