Categories
JavaScript Answers

How to create a new line in JavaScript?

Spread the love

In JavaScript, you can create a new line in a string using the newline character \n. When you include \n in a string, it signifies a line break.

For example, we write:

var multiLineString = "This is line 1.\nThis is line 2.";
console.log(multiLineString);

This will output:

This is line 1.
This is line 2.

You can use the newline character \n within strings in various contexts, such as in console.log() outputs, in HTML elements (for example, setting the innerText or innerHTML properties), or when constructing strings dynamically.

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 *