「ES6」テンプレート文字列に式を埋め込む

2021年10月13日

構文
console.log(` ${式1} test
message ${式2}.`);

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<script>
let x = 3;
let y = 4;
alert('通常の文字列の計算 値1: ' + (x + y) + ' \n値2 ' + (2 * x + y) );
alert(`テンプレートリテラル 値1: ${x + y}
2: ${2 * x + y}`);
</script>
<script> let x = 3; let y = 4; alert('通常の文字列の計算 値1: ' + (x + y) + ' \n値2 ' + (2 * x + y) ); alert(`テンプレートリテラル 値1: ${x + y} 値2: ${2 * x + y}`); </script>
<script>
let x = 3;
let y = 4;
alert('通常の文字列の計算 値1: ' + (x + y) + ' \n値2 ' + (2 * x + y) );

alert(`テンプレートリテラル 値1: ${x + y}  
      値2: ${2 * x + y}`);
</script>

結果

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
通常の文字列の計算 値1:7
2 10
テンプレートリテラル 値1:7
2 :10
通常の文字列の計算 値1:7 値2 10 テンプレートリテラル 値1:7 値2 :10
通常の文字列の計算 値1:7
値2 10

テンプレートリテラル 値1:7
  値2 :10

 

ECMAScript/ES6

Posted by arkgame