JavaScriptでfor文
for (初期値; 条件式; 増減値) {ループ毎に行いたい処理;}
と書くことでループ処理ができる。
以下の例では1から10までをコンソールに表示します。
index.js
for (let i = 1; i <= 10; i++) {
console.log(i)
}
// 1
// 2
// 3
// 4
// 5
// 6
// 7
// 8
// 9
// 10
ちなみに``(バッククオート)の中に、${式}と書くことで、文字列の中に式を展開することができます。 これをテンプレートリテラルと言うそうです。
index.js
for (let i = 1; i <= 10; i++) {
console.log(`hello ${i}`)
}
// hello 1
// hello 2
// hello 3
// hello 4
// hello 5
// hello 6
// hello 7
// hello 8
// hello 9
// hello 10