When I am adding elements in my template string inside a \"for\" loop, it returns the word \"undefined\" at the beginning of the string.
Despite the fact that I initiali
It's not problem of your loop.
You didn't specify any value in variable declaration let templateString
so it's value is undefined
. It works with let templateString = '';
.
// Hey JavaScript, it's string!
let templateString = '';
let objectStock={
A:"A",
B:"B",
C:"C",
D:"D",
}
for(let objectItem in objectStock){
templateString+= `<br><p><b>${objectItem}</b>: ${ objectStock[objectItem] || "empty field."}</p>`
}
console.log("templateString: ", templateString)