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

Dan Emery
Dan Emery
13,841 Points

Confused about JavaScript variables

A quick question that has me a little confused... How come the variable throws out an error when declared with parenthesis.... var quickLength = quick.length();

but works fine without var quickLength = quick.length;

I ask this because I though parenthesis were always needed after a method was declared??

=S

2 Answers

Aaron Graham
Aaron Graham
18,033 Points

Assuming that "quick" is an array in your example, "length" is a property of an array, not a method like "pop()", or "splice()". It does get rather confusing at times. Your best bet is checking documentation when you run into issues like this. I usually use Mozilla.

Edit: To add some clarification, arrays are actually just objects. Think of them being structured like this:

var array = {
  length: 'some length',
  pop: function() {},
  splice: function() {}
  //... additional properties and methods
}

This obviously is just an example, but hopefully it helps understand the difference in how you use the object. When you reference something in the object that isn't a function, you don't use parenthesis.

Dan Emery
Dan Emery
13,841 Points

ok, thanks for your help :)