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

Is it possible to use ternary operator for console.log?

I thought this would be something simple to do, but I don’t think it’s possible, why is that?

This is what I’m trying to do.

console.log("hello") ? bool : "";

This gives me an error: Expected an assignment or function call and instead saw an expression. Not sure exactly what that means.

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

>Solution :

Yes. you have to evaluate the expression at runtime.
Do I like this.

const someBool = true;
console.log(someBool?'hello':'goodbye');
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