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
Mary-Kate OlsenOriginal
2024-12-23 22:21:12262browse

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>
`;

Escaping Newlines

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

"foo \
bar"

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!

Statement:
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