Comparing equality of two numbers using JavaScript Number() function

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

var fn = 20;
var sn = 20;

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

This alert returns a false value. Why is this not returns true?

Answers:

Answer

new Number() will return object not Number and you can not compare objects like this. alert({}==={}); will return false too.

Remove new as you do not need to create new instance of Number to compare values.

Try this:

var fn = 20;
var sn = 20;

alert(Number(fn) === Number(sn));

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.