Difference between let and var
Learn the difference between let and var in javascript - Our Code World
by Carlos Delgado
HTML
<h4>
Buttons with VAR (always 5)
</h4>
<input type="button" id="mybutton1" value="Button 1"/>
<input type="button" id="mybutton2" value="Button 2" />
<input type="button" id="mybutton3" value="Button 3" />
<input type="button" id="mybutton4" value="Button 4" />
<input type="button" id="mybutton5" value="Button 5"/>
<br>
<h4>
Buttons with LET (consecutively ++)
</h4>
<input type="button" id="thebutton1" value="Button 1"/>
<input type="button" id="thebutton2" value="Button 2" />
<input type="button" id="thebutton3" value="Button 3" />
<input type="button" id="thebutton4" value="Button 4" />
<input type="button" id="thebutton5" value="Button 5"/>
JavaScript
'use strict'; // to allow LET statement
for(var i=1; i<5; i++) {
$("#mybutton" + i).click(function () {
alert(i);
});
}
for(let i=1; i<5; i++) {
$("#thebutton" + i).click(function () {
alert(i);
});
}