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

Multiline text when using multiple ternary operators in Template literals

I want to create multiline text using multiple ternary operators within a single Template literal.

const toolTipText = `${2 * 5 === 10 ? "The value is same" : ""}
  ${"a" === "b" ? "" : "Different letters"}`;

For example, in the code above, I want The value is same in one line & Different letters in the next line. However, they are both in the same line. I am basically trying to add lines on text based on conditions.

How to achieve it?

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 :

Try adding \n (the escape sequence for a newline character) after The value is same in the ternary expression. This way, a new line will only show when there are two different lines that need to be shown:

const toolTipText = `${2 * 5 === 10 ? "The value is same\n" : ""}${"a" === "b" ? "" : "Different letters"}`;
console.log(toolTipText);
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