Tyler Dennen
Tyler Dennen

Reputation: 13

Call function inside IIFE

let f = function(x) {
  alert(x)
}

(function() {
  f(1)
}())

Why is this code throwing an error? At first, I thought the problem was connected with the incorrect syntax of the IIFE but then I learned that this syntax is also appropriate

Upvotes: 1

Views: 44

Answers (1)

Nina Scholz
Nina Scholz

Reputation: 386604

This is the one of the rare cases where a semicolon is necessary to separate the function expression from the calling with the following parentheses.

let f = function(x) {
  alert(x)
}; // <-------------------

(function() {
  f(1)
}())

Upvotes: 5

Related Questions