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.

18 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?

2 Likes

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.

6 Likes

What is an attribute?

In JS we call them properties.

object = {
    property_name: 'property_value'    //  property (attribute)
}
1 Like

How does the object keyword console makes and transmit data to the second console black window pane?

Keywords are the same as functions/methods?

The console is the standard output from the editor window, using the console.log() method. The objects don’t have any relation to the JS console.

Not the same, but many of them are built-in functions, others are built-in constructs.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar

1 Like

This course teach constructs?

Things like if statements, for statements, while statements, switch statements, etc. are constructs, not functions. Many of the keywords are expressions or operators, such as, in, of, typeof, etc.

1 Like

Property is the same as a variable?

Similar, but not the same. Variables have scope, properties have object context. When you get into class and constructors you’ll see that object properties are called instance variables, which is where they are similar.

a = 42
b = {
    a: 42
}

a and b are variables, b.a identifies the a property of b.

2 Likes