When I am adding elements in my template string inside a “for” loop, it returns the word “undefined” at the beginning of the string

前端 未结 1 814
无人及你
无人及你 2021-01-28 08:32

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

相关标签:
1条回答
  • 2021-01-28 08:46

    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)

    0 讨论(0)
提交回复
热议问题