0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

JavaScript 文字列の中で変数の値を使う方法 テンプレートリテラル

Last updated at Posted at 2022-06-08

文字列の中で変数の値を使う方法

// 変数
var name = "田中";
var color = "";

console.log(`私は ${name} です。 ${color} チームです。`);
// 私は田中です。赤チームです。

解説

  • 文字列自体をバッククオート 「`」 で囲む。
  • 使いたい変数を 「{}」で囲み、先頭にドルマーク 「$」 をつける。

※変数が配列の場合でも可能。添字を指定して値を使うことも可能。
例 ${hoge[0]}

0
0
2

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?