Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading