1

Why this anonymous function

(function () {
  return ("i am a function")
})()

is equivalent to

function hi () {
  return ("i am a function")
}

the function work with hi()

Also why if I removed the brackets for the anonymous function it does not work

function () {
  return ("i am a function")
}()
4
  • Check my answer to this question: stackoverflow.com/questions/1634268/… Commented Nov 14, 2019 at 14:11
  • You need to read about IIFE medium.com/javascript-in-plain-english/… Commented Nov 14, 2019 at 14:14
  • 1
    If you don't use the parentheses, the function is evaluated as a Function Statement, and function statements cannot be anonymous, the name is required by the grammar. Commented Nov 14, 2019 at 14:15
  • your first one is Immediately invoked function ... so when you declare a function as your hi function after you have to call with hi() ... but your last exaple doesn't work becouse you return a value but you never "call" that so if you change the last as: myMessage=function () { return ("i am a function") }() Commented Nov 14, 2019 at 15:47

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.