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?

3 つの数値を出力 JavaScript編(paizaランク D 相当)

0
Last updated at Posted at 2026-07-31

本記事は、paizaラーニング「レベルアップ問題集」の学習記録です。
同コーナーでは、解答やコードの公開が認められています。

問題

3つの数値 813 を、半角スペース区切りで出力してください。

期待する出力

8 1 3

末尾に改行を入れ、余計な文字や空行を含めてはいけません。

解答

最初は文字列をつなげて出力しました。

console.log(8 + " " + 1 + " " + 3);

しかし、console.log() の引数をカンマで区切るだけでも、半角スペース区切りで出力できます。

console.log(8, 1, 3);

今回の問題では、この書き方が最も簡単でした。

テンプレートリテラルでも書ける

「テンプレートリテラルでも書けるのでは?」と思いましたが、書き方を忘れていたので復習しました。

テンプレートリテラルは、文字列全体をバッククォート ` で囲みます。

変数や式を埋め込みたい場所には、${} を使います。

const a = 8;
const b = 1;
const c = 3;

console.log(`${a} ${b} ${c}`);

出力結果は次のとおりです。

8 1 3

学んだこと

  • console.log() の引数をカンマで区切ると、半角スペース区切りで出力される
  • テンプレートリテラルは、全体をバッククォートで囲む
  • 変数や式は ${} の中に書く
  • 今回のような固定値の出力では、テンプレートリテラルを使わなくてもよい
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?