Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why no () after .length in Javascript?

Tags:

javascript

I'm a hobby programmer, I've studied several languages and almost always find that 'length' is a method/function. I've been trained, from what I can tell, that any method call must be called with a parenthesis after, even with no arguments.

Not so in Javascript.... Why?

C# .length() ROR .lenth()

etc...

like image 945
mike varela Avatar asked Jan 28 '12 00:01

mike varela


1 Answers

In Javascript it's a property, not a function.

In languages like C where the length function has to loop through the entire string to find the end, it's logical to have it as a function (as it does some work). In languages where the length is kept as a separate value, it's logical to have it as a property instead (as it only reads an already existing value).

like image 91
Guffa Avatar answered Oct 04 '22 09:10

Guffa