For Loop help for defining a variable

For Loop help for defining a variable
0

#1

I want to use a for loop to make my code look cleaner :

let drumPad1 = document.getElementById('drum-pad1')
let drumPad2 = document.getElementById('drum-pad2')
let drumPad3 = document.getElementById('drum-pad3')
let drumPad4 = document.getElementById('drum-pad4')
let drumPad5 = document.getElementById('drum-pad5')
let drumPad6 = document.getElementById('drum-pad6')
let drumPad7 = document.getElementById('drum-pad7')
let drumPad8 = document.getElementById('drum-pad8')
let drumPad9 = document.getElementById('drum-pad9')

I tried this

for(let i = 1; i <= 9; i++){
     let drumPad[i] = document.getElementById('drum-pad[i]')
};

but it says that :

Uncaught SyntaxError: Unexpected token [


#2

You can’t declare a variable with brackets in them.

You can try using an array:

let drumPad = [];
for (...) {
  drumPad[i] = document.getElementById('drum-pad' + i);
}