Active March 09, 2022 / Viewed 51 / Comments 0 / Edit
Examples of how to create a string variable on multiple lines in javascript
Summary
To create a string variable on multiple lines in javascript, a solution is to use a template literal which is delimited by backticks ``:
var s = `an example of a very very
very very very very very very
very veryvery very long string`
console.log( s );
Another example with aninnerHTML :
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<div id="intro">
<h1>Learning Javascript</h1>
<p>First paragraph</p>
</div>
<script>
document.querySelector("#intro").innerHTML +=
`<p>Second paragraph</p>
<p>Third paragraph</p>
<p>Fourth paragraph</p>`;
</script>
</body>
</html>
Hi, I am Ben.
I have developed this web site from scratch with Django to share with everyone my notes. If you have any ideas or suggestions to improve the site, let me know ! (you can contact me using the form in the welcome page). Thanks!