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?
(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.
Comments