-2

Why does

console.log(new Number("3") === new Number("3"));

return false?

Similarly, why is

console.log(new Number("3") == new Number("3"))

also false?

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
  • 2
    because they are different objects (because you are using `new` it is constructing number Objects, not just coercing the strings to numbers) see: [Creating Number objects](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/Number#creating_number_objects) it's directly covered in the examples – pilchard Dec 31 '21 at 23:04
  • 2
    I see - so since I'm comparing objects, it will not coerce the objects into values and return true? Weird how objects created with new Number() doesn't coerce in this case... – WanderingDoge Dec 31 '21 at 23:05
  • 1
    @WanderingDoge Both operands have the same type; no coercion ever happens in this case. – Sebastian Simon Dec 31 '21 at 23:09
  • All objects never equal, unless there the same object. Your creating 2 number objects. `new Number(3)` is not the same as the native type `3`.. – Keith Dec 31 '21 at 23:47
  • What's the point of this anyway? Comparing two `int` values should not use `Number` at all. – Martin Zeitler Dec 31 '21 at 23:51
  • Seems like a.toFixed() === b.toFixed(), so that any manipulation to return an actual value (even though it is from different objects) will return a numerical value - so I get it now that I should extract the value first, from the object, instead of directly comparing two objects. Thanks! – WanderingDoge Jan 01 '22 at 07:53

1 Answers1

0

From the docs:

The Number() constructor creates a Number object.

This means that you're checking for the equality of two objects. To check for the primitive values, you can use Number#valueOf:

console.log(
  (new Number("3")).valueOf() === (new Number("3")).valueOf()
);
Majed Badawi
  • 27,616
  • 4
  • 25
  • 48
  • 2
    This is incorrect. You should ***never*** use `new` before `Number()`: http://linterrors.com/js/do-not-use-a-as-a-constructor – HoldOffHunger Dec 31 '21 at 23:10
  • 2
    @HoldOffHunger That's out of the scope of the question, the issue is related to OP using the constructor. – Majed Badawi Dec 31 '21 at 23:13
  • 3
    This is the appropriate solution, imho: `console.log(Number("3") == Number("3"))`. – HoldOffHunger Dec 31 '21 at 23:14
  • Ok, but that doesn't clarify OP's question. Thanks for sharing this, it would be beneficial as well. – Majed Badawi Dec 31 '21 at 23:15
  • 1
    @HoldOffHunger The whole point is that the OP *is* using the constructor, leading to the confusion. Your comments would be better expressed as a comment to the OP or as an answer. – Dave Newton Dec 31 '21 at 23:18
  • @DaveNewton You can't answer a closed question. That is why I put the comment here. P.S. (Not the downvoter here, not that it should matter.) You have enough rep to reopen it, right? I can add the answer if you do that. – HoldOffHunger Jan 01 '22 at 00:49
  • @DaveNewton It most certainly was. By Certain Performance and then now by VLAZ. – HoldOffHunger Jan 01 '22 at 01:04
  • @HoldOffHunger Why does the question need to be reopened? What part of it is not covered either in the three duplicates currently linked to it, nor at all in any other place on SO over the more than a decade since it was created? – VLAZ Jan 01 '22 at 09:48
  • @VLAZ: I'm not the one who said it wasn't or shouldn't be closed. Feel free to direct your questions to that person with an `@` at their name, thanks. (Please don't @ me for someone else's questions.) – HoldOffHunger Jan 01 '22 at 15:55
  • @HoldOffHunger [you've also voted to reopen the question when it was first closed](https://stackoverflow.com/posts/70546107/timeline#history_502abc20-e931-42a7-b27c-2b2d6fe962d5). – VLAZ Jan 01 '22 at 19:21