How does an empty string ("") call a function in Javascript?

I am new to coding, and javascript sometimes has weird syntax. I know many methods for calling a function, but it intrigues me. I went through the code written by the developer, and I understood what was happening, but could not figure out how to do it.

A function is called, placed ("")next to the function definition inside ().

(function functionName() {console.log("Hello")}) ("")
// Hello

(functionName)("")
// evaluates the function

I tried the search but could not find it here. I would be grateful if someone could explain what is happening here (concept). Let me know if this question is duplicated. Thank.

+4
source share
1 answer

You just call the function with functionNameone argument "".

​​:

function functionName() {console.log("Hello")}

:

functionName
(functionName)

(function functionName() {console.log("Hello")})("")

:

    • : , . . , functionName .
  • ( )
  • ""

, , ""?

Javascript / , :

  • ( ), . "" .
  • , undefined.

function test(a, b) { console.log(a + " " + b) }

test()              // undefined undefined
test("a")           // a undefined
test("a", "b")      // a b
test("a", "b", "c") // a b
Hide result
+2

Source: https://habr.com/ru/post/1676523/


All Articles