LoginSignup
0

More than 5 years have passed since last update.

【JavaScript 】配列の全ての要素に対して処理を実行する

Posted at

配列の全ての要素に対して処理を実行する

'use strict';

const a = [0, 1, 2];

// for
for (let i = 0; i < a.length; i++) {
    console.log(a[i]);
}
// 0
// 1
// 2

// forEach
a.forEach(value => {
    console.log(value);
});
// 0
// 1
// 2

a.forEach((value, index) => {
    console.log(`a[${index}] = ${value}`);
});
// a[0] = 0
// a[1] = 1
// a[2] = 2

※Google Chrome で確認
※ES8

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