Home > Web Front-end > JS Tutorial > How Can I Assign Multiline Strings to Variables in JavaScript?

How Can I Assign Multiline Strings to Variables in JavaScript?

Mary-Kate Olsen
Release: 2024-12-23 22:21:12
Original
161 people have browsed it

How Can I Assign Multiline Strings to Variables in JavaScript?

Assigning Multiline Strings to Variables in JavaScript

JavaScript lacks a designated here-document syntax as found in Ruby. However, it does offer alternative methods for representing multiline strings.

ES6 Template Literals

ES6 introduces template literals, a type of literal designated by backticks (`). Template literals can be multiline, as seen in the following example:

var html = `
  <div>
    <span>Some HTML here</span>
  </div>
`;
Copy after login

Escaping Newlines

For older versions of JavaScript (prior to ES6), you can resort to escaping newline characters within a string:

"foo \
bar"
Copy after login

This technique allows for string continuation across multiple lines.

The above is the detailed content of How Can I Assign Multiline Strings to Variables in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template