0

Can anyone explain to me what the following JavaScript expressions mean? and why they yield different results? How do the following two expressions help anyone differentiate value or reference types in JavaScript?

(function () {}) === (function () {})
//=> false

(function () {})() === (function () {})()
//=> true
2
  • 3
    this is not "advanced" at all... Commented Jan 2, 2014 at 16:39
  • Are you happy with the title now???. Commented Jan 2, 2014 at 16:43

2 Answers 2

5
(function () {}) === (function () {})

Compares two functions. Function equivalence is tricky.

(function () {})() === (function () {})()

Compares the results of two functions, both of which return undefined.

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

5 Comments

so (function () {}) is an anonymous self-executing function with no arguments? while the ones in the second expression have arguments for both?
no, (function() {}) is just a function expression (with no arguments) wrapped in parens. (function() {})() is an IIFE of a function with no arguments.
Could I have deleted the wrapper parens to avoid confusion? In first expression? In second expression?
@SariksaThapa. Since, JavaScript function has no signature it's just objects (instances) that created with Function constructor, there's no difference between function with parameters/arguments and without it. So, 2 instances of Function would be always return false when comparing, since their reference value is different and they refer different location in heap.
@SariksaThapa yes in the first case, no in the second. The parens are necessary for the function to be invoked immediately. You can, however, put the invoking parens inside or outside the wrapping parens with no change. (function() {}()) === (function() {})()
0

You create two functions - despite we know they do the same (nothing), those are two different functions.

In the second one we compare returned values - by default - undefined.

Comments

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.