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 JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Using For Loops with Arrays

Mathew Kurian
Mathew Kurian
4,698 Points

Why does the length function in Javascript not have parentheses?

I've noticed that the length function in Javascript does not have parentheses. I understand that it does not take a parameter, like the console.log(), document.write() function, etc.

However there are other functions that don't require parameters that still have parentheses. For example, shift() and pop(). Why is the .length function the only exception here?

4 Answers

A function is a group of statements to perform an action when called on. A property on the other hand holds information. Think of a property as a variable (it is a variable) that variable stores information so in this case the length variable stores the length of an array and the function (method) push and pop does an action they either remove elements from an array or add them. I'm believe you want to know how the length property could know the length of a string with out calling a function? I don't know the answer to that if it's so but think of it like this you create a string

var string = "Hello World!";

when this string is created maybe i dont know but maybe a function is called right away like getLength and it stores it in the length variable. thus using string.length gives the length. Hope I haven't confused you but i'll clear it up if I need to

Mathew Kurian
Mathew Kurian
4,698 Points

No that's great! You really explained it well. Are there any other properties similar to 'length'? Or is that one of the only ones?

Samuel Webb
Samuel Webb
25,370 Points

Yea .length is pretty much all you'll have to worry about on strings. Check out the String object at Mozilla Developer Network under String Instances to get more information. And to be clear, Emeka kept saying function, but when you create a function that belongs to an object, it is actually called a method. This is important so that you'll know what you're looking at when you go to MDN. When you see Methods, just think Functions on an object. And there are a lot of methods on the String object.

In relation to JavaScript objects, words that end with a set of parentheses are methods (which are just functions that are associated with an object). Words that do not end in a set of parentheses are properties (variables that describe the state of the object).

An easy way to think about this is: methods perform actions while properties only describe things.

Length is not a method, it is a property. It doesn't actually do anything but return the length of an array, a string, or the number of parameters expected by a function. When you use .length, you are just asking the JavaScript interpreter to return a variable stored within an object; you are not calling a method.

For example:

var x = "hello world"

x.length  -> 11    // This is a property. It just describes the state of the variable.
x.toUpperCase()  -> "HELLO WORLD"    // This is a method. It actually performed an action 
                                     // and returned an independent piece of data.

EDIT - I see someone answered this question with a similar example before I could submit my post. I'll leave this answer up in the hopes it provides you with some clarity.

Mathew Kurian
Mathew Kurian
4,698 Points

Hey, thanks Daniel. Great answer! Do you know if there are any other properties besides the length property? Are properties kind of universal? Or do arrays and strings each have different properties?

EDIT: That explains everything so much better Daniel. Thanks for the explanation. And thanks Karis! I didn't know that, but that's an important detail. I feel like it's one of those things that will save you from spending hours looking for a bug you can't find.

"JavaScript is designed on a simple object-based paradigm. An object is a collection of properties, and a property is an association between a name and a value. A property's value can be a function, in which case the property is known as a method. In addition to objects that are predefined in the browser, you can define your own objects."

To answer your question in fewer words: Everything that is associated with an object, but isn't a method, is a property.

var myObject = {
    thisIsAProperty: 0,
    thisIsAlsoAProperty: true,
    everythingThatIsNotAMethodInAnObject: "Is a property.",

    notAProperty: function () {
       return "This is a method."
    }
}

myObject.thisIsAProperty  -> 0
myObject.thisIsAlsoAProperty  -> true
myObject.everythingThatIsNotAMethodInAnObject  -> "Is a property."

myObject.notAProperty()  -> "This is a method."

Also, note that the String.length property gives the actual number of code units in a string, rather than a literal character count. One code unit is 16 bits as defined by UTF-16 (used by JavaScript). However, some special characters use 32 bits which means that in a string containing one of these characters the String.length property might give you a higher character count than the literal number of characters.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length

The "length" is not a function it's a property of the string object

Mathew Kurian
Mathew Kurian
4,698 Points

How are properties different from sub-functions? Can you give some other examples?

lol, jesus help me. I mean, hey it's so simple...why even ask? For all you smart people that is sarcasm, this is very hard for me. love you all :D