I have a basic question about for-loops and variables. I have the following code:
function fiveLine(s){
let trim=s.trim()
let fulltrim;
for (let i=1; i<=5; i++){
fulltrim+="\n"+trim.repeat(i)
}
return fulltrim
}
When invoked, with fiveLine('a') it prints
undefined
aaa
aaaaaa
aaaaaaaaa
aaaaaaaaaaaa
aaaaaaaaaaaaaaa
Besides the output 'undefined' it works perfectly like I want it to. I understand that the variable fulltrim is accessed and returned before it is updated by the for-loop, but I don't understand why. In other words, I don't understand why the output is not
aaa
aaaaaa
aaaaaaaaa
aaaaaaaaaaaa
aaaaaaaaaaaaaaa
For me, the variable fulltrim is already updated by the for-loop when returned for the first time, thus it shouldn't be undefined anymore. Thanks for reading!
fulltrim. Uselet fulltrim = "";.