Jeremy Beare
Jeremy Beare

Reputation: 487

Why does `String.prototype.concat` not modify the string in place?

I've been trying to create a news box for my website today and I have a problem. Let me explain what happens. I create a 2d array that contains the news (date and the news). I then loop through it to construct the news_string and then use that for the DIV's innerHTML. I have put a very simple version of it below

for (var i = 0; i < news.length; i++) 
{
news_string.concat(news[i][1],"<br>");
}

document.getElementById("news-content").innerHTML = news_string;

However nothing appears. I have cut it down to the very minimal. No result. I have used alerts. Nothing appears. The news_string is blank regardless of the fact I put data into it. And even if I do gain a string nothing appears in the DIV box. What's causing this massive break?

Upvotes: 0

Views: 86

Answers (1)

Sergio
Sergio

Reputation: 28845

The concat method returns a value, you have no variable assignement there to catch it...

From the docs (notice the bold part):

The concat() method combines the text of two or more strings and returns a new string.

So you should use:

news_string = news_string.concat(news[i][1],"<br>");

Upvotes: 1

Related Questions