Comparing equality of two numbers using the JavaScript Number () function

When I try to compare two numbers using the JavaScript Number() function, it returns false for equal numbers. However, operations with a grater than (">") and "less than" ("<") return true .

 var fn = 20; var sn = 20; alert(new Number(fn) === new Number(sn)); 

This warning returns false . Why does this not return true ?

+8
javascript
source share
1 answer

new Number() will return object not Number , and you will not be able to compare objects like this. alert({}==={}); will also return false .

Remove new , since you do not need to create a new instance of Number to compare values.

Try the following:

 var fn = 20; var sn = 20; alert(Number(fn) === Number(sn)); 
+11
source share

All Articles