DEV Community

loading...

New Number() is truthy or falsy?

318097 profile image Mehul Lakhanpal Originally published at codedrops.tech ・1 min read
const zero = new Number(0);

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

Discussion (3)

pic
Editor guide
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
318097 profile image
Mehul Lakhanpal Author

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

Collapse
kannandv profile image
kannan DV

zero is truthy,