Categories
JavaScript Answers

How to create a JavaScript string with new line but not using \n?

Spread the love

Sometimes, we want to create a JavaScript string with new line but not using \n.

In this article, we’ll look at how to create a JavaScript string with new line but not using \n.

How to create a JavaScript string with new line but not using \n?

To create a JavaScript string with new line but not using \n, we can use template literals.

For instance, we write

const foo = `Bob
is
cool`;

to define the foo template literal.

Then whitespaces and new lines are preserved with template literals.

Conclusion

To create a JavaScript string with new line but not using \n, we can use template literals.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.