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 trialAnupam Kumar
940 PointsThese two same loops provides different output pattern on console could not understand why?
var name = ['Anupam','AnuPriya','Apoorva']; /* loop 01*/ for( var i=0;i<name.length;i+=1) { console.log(name[i]);
} /* loop 02*/ var orderQueue = ['Anupam','AnuPriya','Apoorva']; for( var i=0;i<orderQueue.length;i+=1) { console.log(orderQueue[i]); }
3 Answers
Dom Farnham
19,421 PointsCan you share the output? Do you have both loops in the same js file? If so, instead of using i in the 2nd loop, replace i with j.
Let me know how you get on. Hope this helps. :-)
Dom Farnham
19,421 PointsI don't think it is a good idea to call your array 'name'. Can you replace name with something else, like names, and see if the output from console.log is different.
Seth Kroger
56,413 PointsThe trouble is name
ends up being a single string instead of an array. The window
object which supplies bunch of predefined global variables already has a name
property which is an empty string. Because name already exists as a string, it will try to coerce the value you try to assign to a string.
This problem occurs when using var
but not the new let
/const
keywords
Anupam Kumar
940 PointsThanks Seth, for helping me out; It worked
Anupam Kumar
940 PointsAnupam Kumar
940 PointsHey Dom, Please find below the outputs , I even tried with changing the variables with j, but it gave same result on console. I got desired result on web, but curious to know why console gives these different outputs for same loops /* loop 01*/ var name = ['Anupam','AnuPriya','Apoorva']; for( var i=0;i<name.length;i+=1) { console.log(name[i]); } Output A n u p a m , A n u P r i y a , A p 2o r v a
/* loop 02*/ var orderQueue = ['Anupam','AnuPriya','Apoorva']; for( var i=0;i<orderQueue.length;i+=1) { console.log(orderQueue[i]); } Anupam AnuPriya Apoorva undefined