javascript - Why this 'undefined'
Get the solution ↓↓↓Solution:
Break it down to the problem:
var rowText; //undefined
rowText += "Hello"; //undefined + "Hello";
console.log(rowText); //"undefined Hello"
If you set it to an empty string, the undefined issue will go away
var rowText = "";
Answer
Solution:
My guess is thatrowText
isundefined
.
var rowText; // The variable exist but it's value is undefined until a proper value is given to it.
for(var index in rowData)
{
rowText += rowData[index];
}
document.getElementById('rows').innerHTML = rowText;
IfrowData
is empty (you should dump the value of it to make sure) thenrowText
will beundefined
.
It would make more sense to give it an initial value of the empty string, that should give you the empty string instead ofundefined
.
Also, take a look at why you shouldn't use innerHTML.
Answer
Solution:
You might have a function on rowData, so try to check with HasOwnProperty whether rowData has the property before adding to text:
for(var index in rowData)
{
if(rowData.hasOwnProperty(index)){
rowText += rowData[index];
}
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: call to a member function format() on string
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.