DEV Community

Mehul Lakhanpal
Mehul Lakhanpal

Posted on • Originally published at codedrops.tech

New Number() is truthy or falsy?

const zero = new Number(0);

if (zero) {
  console.log("zero is truthy!");
} else {
  console.log('zero is false!');
}
Enter fullscreen mode Exit fullscreen mode

Top comments (3)

Collapse
 
nuculabs_dev profile image
Nucu Labs

Oh, JavaScript and it's mysterious ways.

const zero = new Number(0);

if (zero) {
  console.log("zero is truthy!");
} else {
  console.log('zero is false!');
}
zero is truthy! base.js:8272:41
undefined
if (0) {
  console.log("zero is truthy!");
} else {
  console.log('zero is false!');
}
zero is false!
Enter fullscreen mode Exit fullscreen mode
Collapse
 
ml318097 profile image
Mehul Lakhanpal

new Number() creates a object wrapper. So it turns out to be true

Collapse
 
kannandv profile image
kannan DV

zero is truthy,