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 trialMike Zhu
1,840 PointsWhy can not use such description like "for (int i = 0; i < friends.length; i++)"in the loop
When I try to use for (int i = 0; i < friends.length; i++), it just cannot run the loop. I have to use for (var i = 0; i < friends.length; i++)
Is it because in Javascript, int i = 0, is just a constant? Only var i = 0, is a variable?
2 Answers
chrisp
13,686 PointsHI Mike,
Of course, no problem. You're very welcome :).
Yes you're right. In JavaScript 'int' is not really a datatype, but its more of a reserved word.
Unlike other programming languages, reserved words like 'int', 'long'. 'float', etc... are reserved words that are currently not use.
In later version of javascript, I remember that 'int' is no longer a reserved word and has been taken out and therefore is best to avoid using it.
Here is a list of javascript reserve words http://msdn.microsoft.com/en-us/library/ie/0779sbks(v=vs.94).aspx
Here is a list of datatypes http://msdn.microsoft.com/en-us/library/ie/7wkd9z69(v=vs.94).aspx
Best of luck with Javascript,
chrisp
13,686 PointsI think when you are using 'int' with 'i' it returns false because 'i' is not an integer and int doesn't really store anything. Javascript would get confuse when 'i' hasn't been introduce or define.
var is the way to go since it can hold many datatypes.
In most cases in javascript nothing is constant as in a variable that cannot be changed. Defining Const is still a draft version for ECMAScript 6. I am not sure if browser are currently supporting it.
Mike Zhu
1,840 PointsThank you so much! But in other programming languages, such as Java or Objecttive-C, the for-loop goes like, int i = 0; i < 5; i++. I am so confused that, just like you said, "i" is not an integer, so, you mean, in Javascript, "int" in not a data type for integer, is that right? Because in other languages, "int" stands for the data type of an integer.
I am completely new in Javascript and thanks for your answer!