Comparing equality of two numbers using JavaScript Number() function

后端 未结 2 1333
春和景丽
春和景丽 2021-01-17 12:01

When I try to compare two numbers using JavaScript Number() function, it returns false value for equal numbers. However, the grate

2条回答
  •  失恋的感觉
    2021-01-17 12:36

    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));

提交回复
热议问题