LoginSignup
0
0

More than 1 year has passed since last update.

Javascript:テンプレートリテラル

Posted at

テンプレートリテラル

文字列を結合する場合、テンプレートリテラルを使用することを推奨。
テンプレートリテラルを使用すると、コードがより読みやすくなり、エラーを防ぎやすくなる。  
下記のように、文字列をグレイヴ・アクセント(`)で囲み、変数を入れるときはドル記号と波括弧でくくることで+で文字列結合などを行わず簡潔に表示内容を記述することができる。

function replaceLastFourWithHint(stringInput) {
  return stringInput.length < 6
    ? "There is no Hint"
    : `Hint is: ${stringInput.slice(-4)}`;
}

以下のように、'\n'(改行コード)を記述せずとも直感的に記述できる。

console.log(`string text line 1
string text line 2`);
// "string text line 1
// string text line 2"

その他、便利な記法により読みやすくすることができる書き方があるので、MDNを参考に学んでいきたい。
https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Template_literals

0
0
0

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
0
0