Skip to content Skip to sidebar Skip to footer

When I Am Adding Elements In My Template String Inside A "for" Loop, It Returns The Word "undefined" At The Beginning Of The String

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 initialize all my objec

Solution 1:

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)

Post a Comment for "When I Am Adding Elements In My Template String Inside A "for" Loop, It Returns The Word "undefined" At The Beginning Of The String"