複数行の文字列を作成するJavaScriptプログラム

Javascript

目次

例1:+を使用して複数行の文字列を作成する

// program to create a multiline strings

// using the + operator
const message = 'This is a long messagen' + 
    'that spans across multiple linesn' + 
    'in the code.'

console.log(message);

出力

This is a long message
that spans across multiple lines
in the code.

上記の例では、複数行の文字列は + 演算子と n

エスケープ文字 n 線を切るために使用されます。


例2:を使用して複数行の文字列を作成する

// program to create a multiline strings

// using the  operator
const message = 'This is a long messagen 
that spans across multiple linesn 
in the code.'

console.log(message);

出力

This is a long message
that spans across multiple lines
in the code.

上記の例では、複数行の文字列はを使用して作成されます n 線を切るために使用されます。


例3:テンプレートリテラルを使用して複数行の文字列を作成する

// program to create a multiline strings

// using the template literal

const message = `This is a long message
that spans across multiple lines
in the code.`

console.log(message);

出力

This is a long message
that spans across multiple lines
in the code.

上記の例では、テンプレートリテラル ` ` 複数行の文字列を書き込むために使用されます。

テンプレートリテラルは、JavaScriptの新しいバージョンで導入されました(ES6)。

一部のブラウザは、テンプレートリテラルの使用をサポートしていない場合があります。 詳細については、次のWebサイトをご覧ください。 JavaScriptテンプレートリテラルサポート



Hope this helps!

Source link

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です