Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

145
Views
js types of Bool conversion

Do all 3 ways use the same conversion to bool?

function check(variable){
    let b1 = Boolean(variable);
    let b2 = !!variable;
    let b3 = variable ? true : false;
    
    return b1 === b2 && b2 === b3;
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Do all 3 ways use the same conversion to bool?

Yes. The Boolean function, the logical NOT operator and the conditional operator all convert the value to a boolean value via the internal ToBoolean algorithm:

enter image description here

(of course engines are free to implement it however they want, but it has to behave like the spec dictates)

about 4 years ago · Juan Pablo Isaza Report

0

MDN Web Docs tell us the following:

Do not use a Boolean object to convert a non-boolean value to a boolean value. To perform this task, instead, use Boolean as a function, or a double NOT operator:

const x = Boolean(expression);     // use this...
const x = !!(expression);          // ...or this
const x = new Boolean(expression); // don't use this!

According to the docs, the first form is equivalent to the second form and looking at the second form, you could deduce that it is equivalent to the third form you suggested.

So all three of your ways should be equivalent and they are dependent on what JavaScript considers as a truthy value

As to why you shouldn't use new Boolean() to convert something to boolean, this section offers a bit of an explanation. TL;DR You might accidentally initialize non-truthy values as truthy.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!