LoginSignup
67
51

More than 5 years have passed since last update.

JavaScriptで文字列を複数行に記述するやり方

Last updated at Posted at 2014-09-10

JavaScriptにはヒアドキュメントがありませんが、
下記のように行末に\を付けることで複数行の文字列を変数に代入することが出来ます。

sample.js
var listCode = '\
     <li class="" data-pref-name="{{name}}" data-is-selected="false">\
          <span class="name">{{name}}</span>\
          <span class="count">{{coount}}</span>\
     </li>';

追記

noyaさんのコメントにある通り、
Google JavaScript Style Guideでは\による文字列の連結は使わない方が良いとありました。
※というより、明確に「このような書き方は禁止です」と・・・

こちらの和訳ではECMAScript5では標準化されたとあるので、
この記事は一応残しておきます。
http://cou929.nu/data/google_javascript_style_guide/#string

noyaさん、有り難うございました。

67
51
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
67
51