4

I am new to coding and javascript has some strange syntax at times. I know many methods of calling a function but this one intrigues me. I went through a code written by a developer and I deduced what was happening but couldn't understand how.

A function is called by placing ("") next to the function definition within ().

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

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

I tried searching but couldn't find it here. I would be grateful if someone can explain what is going on here (the concept). Let me know if this question is duplicated. Thanks.

5
  • 2
    Nothing special is going on. (functionName) just evaluates to the function, and ("") calls the function. In JavaScript, functions can be values, and can be used in expressions. Commented May 6, 2017 at 1:56
  • 4
    Addition is + “next to” some numeric expressions, as in (2 * 3) + (4 - 5) === 5. Negation is ! “next to” a boolean expression, as in !(1 > 2) === true. A function call is (…) “next to” a function expression, as in (Math.max)(10, 100) === 100. Commented May 6, 2017 at 1:56
  • Aha... Now I feel stupid. It's so clear. Only thing, the function does not take any argument. So a "" kinda confused me. Commented May 6, 2017 at 2:20
  • 1
    Note that the empty string passed in the call isn't strictly relevant to the question, and it's ignored in the function. It would work the same without it. Commented May 6, 2017 at 2:21
  • Here the parameter set to nothing so it will not do anything inside the function only write empty string to console then inside the function you have to do your validation using (case and status) or (if and else) and this depend on the result you want from your function to achieve your task for me programming is all about how to use logical statement and build in function and data types to do the tasks you want. Commented May 6, 2017 at 2:44

1 Answer 1

2

You're simply calling the function functionName with a single argument "".

If you define a function like this:

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

Then those two statements are equivalent:

functionName
(functionName)

When you type

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

You:

  • define an anonymous function
    • Note: because it has parentheses around its declaration, the scope of the function is restricted. The function won't be available to the rest of the program. It means that functionName will not be defined on the next line of code.
  • evaluate the function (by putting parentheses around the function definition)
  • call the function, with a single argument ""

The function doesn't accept an argument, so why does it work when I pass the argument "" to it?

Javascript allows calling functions with more/less arguments then they accept:

  • If there are too many arguments (like in our case), the extra arguments are discarded. In this case, the "" is discarded.
  • If there are not enough arguments, they are 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

Sign up to request clarification or add additional context in comments.

4 Comments

I removed my downvote and gave you an upvote instead, but it should be noted that (function functionName() {console.log("Hello")}) does not declare any function. Instead, it is defining an anonymous function which behaves differently than a declared function. The name functionName is not available to the rest of the program is because the function is not a declared function.
@Derek朕會功夫 I added another correction, thank you. Hopefully this time it is correct.
That was even more helpful. Can you please explain why an empty string works here. The function does not take any argument so what does this empty string mean?. Thanks.
@Paul I added an explanation of why "" gets ignored

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.