What's the difference between these 2 comparisons?

shmuli

What's the difference between this...

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

and this...

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

The prior comparison evaluates to true, yet the latter evaluates to false. Why is that?

Sebastian Simon
(function () {})()

is an IIFE (immediately invoked function expression). This means, it is a function that gets immediately executed and the actual value of it is its return value. This function doesn’t have a return statement, therefore the value is undefined. undefined === undefined is true.

(function () {})

however, is a function. A function is not a primitive value, but like an object. In JavaScript, when comparing objects (or functions), the references are compared. Because both (function () {})s create a new function, these aren’t the same function, hence the comparison yields false.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What's the difference between single and double equal signs (=) in shell comparisons?

From Dev

What's the difference between these 2 ifs?

From Dev

What's the difference between these 2 sorting methods?

From Dev

What's the difference between "as?", "as!", and "as"?

From Java

What's the difference between '$(this)' and 'this'?

From Dev

What's the difference between "./" and "../"?

From Dev

What's the difference between "==" and "=~"?

From Dev

What's the difference between $@ and $*

From Dev

what's difference between these

From Dev

What's the difference between([])+ and []+?

From Dev

What's the difference between LayoutInflater's Factory and Factory2

From Dev

What's the difference between rxjava2's Maybe and Optional?

From Dev

What's the difference between LayoutInflater's Factory and Factory2

From Dev

what's the difference between .ivy2 and .m2

From Dev

what is the difference between >&2 and &>2

From Java

What's the difference between these 2 ways of using a callback in addEventListener?

From Dev

What's the difference between these 2 implementations of prototypal inheritance?

From Dev

What is the difference between Python's unittest and unittest2 modules?

From Dev

What's the difference between Yii 2 advanced application and basic?

From Dev

What's the difference between vimdiff and vimdiff2 in git?

From Dev

what's the difference between bitwise shift with 2 arrows and 3 arrows?

From Dev

Java - what's the difference between 2 little codes

From Dev

What's the difference between Restsharp vs Restsharp.net2

From Dev

What's the difference between these 2 Angular code snippets?

From Dev

What's the difference between 2 Java8 downloads?

From Dev

What's the difference between these 2 icons in TortoiseSVN Repo Browser?

From Dev

What's the difference between the 2 ways of showing window in Swing?

From Dev

What's the difference between these examples (* and &)?

From Dev

What's the difference between <<, <<< and < < in bash?

Related Related

  1. 1

    What's the difference between single and double equal signs (=) in shell comparisons?

  2. 2

    What's the difference between these 2 ifs?

  3. 3

    What's the difference between these 2 sorting methods?

  4. 4

    What's the difference between "as?", "as!", and "as"?

  5. 5

    What's the difference between '$(this)' and 'this'?

  6. 6

    What's the difference between "./" and "../"?

  7. 7

    What's the difference between "==" and "=~"?

  8. 8

    What's the difference between $@ and $*

  9. 9

    what's difference between these

  10. 10

    What's the difference between([])+ and []+?

  11. 11

    What's the difference between LayoutInflater's Factory and Factory2

  12. 12

    What's the difference between rxjava2's Maybe and Optional?

  13. 13

    What's the difference between LayoutInflater's Factory and Factory2

  14. 14

    what's the difference between .ivy2 and .m2

  15. 15

    what is the difference between >&2 and &>2

  16. 16

    What's the difference between these 2 ways of using a callback in addEventListener?

  17. 17

    What's the difference between these 2 implementations of prototypal inheritance?

  18. 18

    What is the difference between Python's unittest and unittest2 modules?

  19. 19

    What's the difference between Yii 2 advanced application and basic?

  20. 20

    What's the difference between vimdiff and vimdiff2 in git?

  21. 21

    what's the difference between bitwise shift with 2 arrows and 3 arrows?

  22. 22

    Java - what's the difference between 2 little codes

  23. 23

    What's the difference between Restsharp vs Restsharp.net2

  24. 24

    What's the difference between these 2 Angular code snippets?

  25. 25

    What's the difference between 2 Java8 downloads?

  26. 26

    What's the difference between these 2 icons in TortoiseSVN Repo Browser?

  27. 27

    What's the difference between the 2 ways of showing window in Swing?

  28. 28

    What's the difference between these examples (* and &)?

  29. 29

    What's the difference between <<, <<< and < < in bash?

HotTag

Archive