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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *