Question:
How to include spaces between text to enter the consecutive text in next line in javascript?

If you want to include spaces between text to move to the next line in JavaScript, you need to use a line break character or a combination of a line break and spaces. There are different ways to achieve this:

  1. Using Line Breaks:

You can use the newline character (\n) to move to the next line. For example:

var multiLineText = "Hello\nWorld";

console.log(multiLineText);

// Output:

// Hello

// World


  1. Using Template Literals:

Template literals allow you to include line breaks directly within the string using backticks:

var multiLineText = `Hello

World`;

console.log(multiLineText);

// Output:

// Hello

// World


  1. Using String concentration:

You can use string concatenation to add line breaks between strings:

var multiLineText = "Hello" + "\n" + "World";

console.log(multiLineText);

// Output:

// Hello

// World


Let’s understand this with an example:

Presently, Our output is looking like this


After doing some minor changes in our javascript code:

Javascript code:

// Get the submit-code button element

var submitCodeButton = document.getElementById("submit-code");


// Get the next element after the submit-code button

var nextElement = submitCodeButton.nextElementSibling;


// Add a click event listener to the submit-code button

submitCodeButton.addEventListener("click", function() {

    // Get the textarea element and set its value to the text content of pre code

    document.querySelector("pre code").textContent = document.querySelector("textarea").value;

});


// Add a click event listener to the next element after the submit-code button

nextElement.addEventListener("click", function() {

    // Get the textarea element and set its value to the text content of pre code

    document.querySelector("textarea").value = document.querySelector("pre code").textContent;

});


pre { white-space: pre; }


textarea {

  height: 240px !important;

}


p {

  font-weight: bold;

}


  

Textarea Output Pre Code