dhairyashah
Portfolio

Nov 22nd, 2022

How to add a new line in Javascript

Author Picture

Dhairya Shah

Software Engineer

Javascript can be used to display data on the front end and on the server. So, there are two ways to add a line break:

Frontend

<html>
  <head>
    <title>Hello World</title>
  </head>
  <body>
    <script>
      document.write("hello")
      document.write("<br />")
      document.write("world")
    </script>
  </body>
</html>

Server

To log data on the server, we must use console.log(). To line break inside the console.log() we can’t use the <br /> inside it. Therefore, here comes a \n which breaks the line inside the console:

 let greet = "hello\nworld"
console.log(n)

Thanks for reading

Follow me on Twitter

Thanks for reading!