1

In this snippet of code I'm defining two functions, outputting them and their string representations to console, and also comparing them:

var a = function func() {},
    b = function func() {};

// string representations are equal
console.log(a.toString());
console.log(b.toString());
console.log(a.toString() == b.toString());
console.log(a.toString() === b.toString());

// functions seem to be equal
console.log(a);
console.log(b);

// but they're not really as this prints false
console.log(a == b);
console.log(a === b);

Why are they not equal?

Same happens of course to empty plain objects, i.e. {}.

Funny thing though is that lodash isEqual returns true in this case: _.isEqual({}, {}); and false in this: _.isEqual(function () {}, function () {}). But of course it's not any proof, it's just the way of implementation of equality.

Karol
  • 7,803
  • 9
  • 49
  • 67

6 Answers6

5

Functions are nothing else than objects, so it all comes back to the comparison of 2 objects, and then according to MDN:

The equality operator converts the operands if they are not of the same type, then applies strict comparison. If both operands are objects, then JavaScript compares internal references which are equal when operands refer to the same object in memory.

Also the full equality comparison algorithm is described in ECMAScript® Language Specification, and 1.f is obvious:

Return true if x and y refer to the same object. Otherwise, return false.

So by design objects are always compared using reference, which is kind of a pointer to the address in memory. If it points to the same, it will return true, if not false is produced.

Karol
  • 7,803
  • 9
  • 49
  • 67
2

This is because functions are objects. In javascript, you cannot test objects for equality like

obj1 == obj2
obj1 === obj2

See Object comparison in JavaScript for how to correctly compare objects

Community
  • 1
  • 1
AmmarCSE
  • 30,079
  • 5
  • 45
  • 53
1

Just like how Two empty boxes aren't equal and how two twins aren't the same person.

Sure the functionality of two functions is equal but you can't test that with equal operators alone.

Functions are objects, just like two human twins are different things. Each function is a different thing.

You meant to compare their value but you are measuring their existence. If that makes sense.

For numbers its simple, their value is their existence. But for ordinary objects. It's not decided what their value is. You could compare all properties of an object against other and see if they all match. Then you could say they are identical objects but still can't say they are the same.

Same thing with strings, their value is definite. String value is implied to be the text that it's holding. And when comparing two string compiler goes over every character. Even though string may have other properties, they dont matter to js compiler. And it's a good design decision.

so when you convert function's definition to string. It happens to be equal to definition of the 2nd function if compared character by character, and this is what == operator will do when dealing with strings, will result in truthy value.

Muhammad Umer
  • 17,263
  • 19
  • 97
  • 168
0

when you compare 2 function, object, it'll compare the reference (address) of variables, not the value.

var a = function() {}, b = function() {};
(typeof a === typeof b) // true both are 'function'
(a() === b()); // true, both are undefined
(a === b) // false, different reference( address in memory).

/* more info */
var a = b = function() {};
(a === b) // true, they've same reference

 var objA = objB = { hi : 'hello'};
 objA === objB // true;
 objA.hi; // 'hello'
 objB.hi; // 'hello'
 objA.hi = 'aloha';
 objB.hi; // 'aloha';

 objB = null;
 objA === objB ; // false, objB was set new value and no longer hold the reference as objA anymore
console.log('now you got it!');

Hope it could help :).

Kai
  • 3,104
  • 2
  • 19
  • 30
0

Functions are considered as objects. They may be identical when you try to log

const a = (c) => c
const b = (c) => c

console.log(typeof a) // returns "function"
console.log(typeof b) // returns "function"

Their types both returns "function". Another example is this

console.log(a(7)===b(7)) // returns true

Both types and returns are the same. However, it returns false when you equate it with double equals

console.log(a == b) // returns false

because every function has different id or instance.

-2

Functions are first class object citizens in the JavaScript world.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445