How to Create a New Line in JavaScript?

Estimated read time 1 min read

In JavaScript, you can create a new line in a string by using the newline character \n. Here’s an example:

const newLine = 'Line 1\nLine 2\nLine 3';

When you run this code, it will output the following:

Line 1
Line 2
Line 3

As you can see, each line is separated by a newline character, which is represented by \n. This allows you to create strings with multiple lines.

You can also create a new line by using template literals. Here’s an example:

const newLine = `Line 1
Line 2
Line 3`;

This code will produce the same output as the previous example. The main difference is that template literals use backticks (“) instead of quotes (” or ‘), and they allow you to embed expressions and newlines directly in the string.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply