Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Bozhong Tao
Bozhong Tao
18,365 Points

Concatenation error in video?

notice from 2:11 - 2:19 in this video: https://teamtreehouse.com/library/ajax-basics/ajax-and-apis/displaying-the-photos

the concatenation in the $.each function, it is written like this:

$.each(array, function(index, item) {
    alert('item ' + index + ' is ' item);
});

however, in the for loop example after this(2:21 - 2:30), it is written like this:

for (var i = 0; i < array.length; i ++) {
    alert('item ' + index + ' is ' + item);
};

notice the concatenation difference in each example, the one using jQuery function didn't have '+' between ' is ' and item variable. Is this a typo or this will produce the same result? Thanks for the advise.

2 Answers

andren
andren
28,558 Points

Missing the + between ' is ' and item inside the $.each method is indeed a typo.

When concatenating values you always need to use the + operator to merge two things together, using a space as shown in your code example will not work. If the code shown in the video had actually been executed you would have received a SyntaxError.

Bozhong Tao
Bozhong Tao
18,365 Points

Thanks andren ! Yes it took me some time to figure out this is indeed a typo. Thought it was some syntax I missed out in the first place. CC Dave McFarland

Adam Beer
Adam Beer
11,314 Points

Actually they do the same thing, just a little different. The for loop is the popular.

jQuery.each()

or

for statement sΓ­ntax

Check this course JavaScript Loops, Arrays and Objects

Bozhong Tao
Bozhong Tao
18,365 Points

Hi Adam Beer , thanks for the feedback. However, I am aware that the two does the same job :)

What am trying to point out is the potential concatenate error in the video. (If it is indeed a typo)