Difference between a property and a method?

Hello , one basic question :
console.log(“Hello”.length)
.log is a method
.length is a properties

So properties = method ?
else what the difference between ?

A method is an attribute, but that does not make an attribute a method. A method is function, so performs some task. .length is a value, only.

We poll values, and call (or invoke) methods.

16 Likes

Follow up question. I’ve seen that “accessors” inside classes can be used as properties. For example,

var book = {
  pageCount: 314,
  get length() {
    return this.pageCount;
  }
};

And this can be used as book.length;

Here, is length a method or a property or both?

1 Like

It is an accessor property (as opposed to data property), but also a special method. As written above it is a little bit deceptive, though, since .length is a property of iterable objects such as arrays and strings. If a reader sees this, book.length they might be led to believe that book is one of those, and not a plain object (object.length => undefined).

Bottom line, the language may let us do a lot of things, but some of them are better left in the drawer.

5 Likes