DEV Community

Sujit Kar
Sujit Kar

Posted on

Why n=0 is falsy but n = new Number(0) is truthy In Javascript ?

let n = 0;
if (n) console.log("Truthy");
else console.log("Falsy"); // It print "Falsy"
Enter fullscreen mode Exit fullscreen mode

Okay fine. But what wrong in the below code?

let n = new Number(0);
if (n) console.log("Truthy"); // it print "Truthy".
else console.log("Falsy");
Enter fullscreen mode Exit fullscreen mode

The answer is very simple.

Let's check data type of the variables.

let n = 0;
console.log(typeof n); It print "number"
Enter fullscreen mode Exit fullscreen mode
let n = new Number(0);
console.log(typeof n); It print "object"
Enter fullscreen mode Exit fullscreen mode

Did you get your answer?

It is happening because of data type is differnt.

Thank You,
@sujitkar1195

Top comments (0)