Difference between a property and a method?

@mtf When you say that scope is the environment where variables can be used for read and write operations, what are read and write operations? And are there any environments where code can’t be used for read and write

A write operation would be when a variable is defined by the assignment of a value, or redefined/updated if it already exists.

A read operation is only a matter of polling a given variable or data structure by reference.

Seems it would be counterproductive, in the main. The whole premise of a program is the movement and dissemination of data.


As to where we are allowed to read from and write to variables, well, that will be its scope. Where it is determines that. A virtual Russian Doll, as it were. The very innermost have access to everything above them; and, the very outermost have access to nothing within.

Could you explain what polling is?

Polling is the act of referencing data.

let a = 42
console.log(a)

The second line is a function which argument polls a variable.

1 Like

Oh I see, would console.log("string".length); be a poll?

Yes. We are polling the length property of the string object.

Polling is a direct request of the environment to deliver up a value. The interpreter will take that to mean go all the way to the top to satisfy this request, else respond, undefined. We can poll from anywhere, but must expect what we poll to exist in some local or higher scope. We are not mutating anything, simply asking for some value in response.


The counterpart, though one would not consider it an opposite, as such, is a query which doesn’t reference anything in particular but rather seeks a match with an object somewhere in the wild; i.e., the sample.

a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
b = a.filter(x => ! (x % 2))

The above is a query of the a array for values that meet the condition given in the filter callback. We would expect all the values in b to be even parity.

Equate the term, query to the term, match. A query is not a pollable thing since we have no reference to begin with. Hence, we query our data structure for possible matches. Those matches can then be made pollable.

Even though query and polling are opposites, couldn’t you interpret query as a response of a request to match something? (Also, what does you’re second line do?)

You could, indeed. A query only returns matches or near matches.

query - Google Search

It may mean jumping ahead of where you are in the learning path, so count this as something of a sneak peek…

You will learn about a special type of function called an iterator. The name refers to methods that accommodate a function known as a callback which the iterator will call upon as it iterates its context object.

context.iterator(function (x) {
    // code to execute on each iteration
})

Glossing over that, the line of code you ask about is acting on the context of object, a with the Array.filter() method that takes a predicate function as its argument. The net effect is to return an array of even numbers. It is an example of matching data to criteria so that only matches are returned. It’s a slightly different form of query, but a query none the less.

1 Like

What’s a callback? And I don’t really understand what you’re definition of iteration is, but would an iteration be the same as a while or for loop? (I’m not sure those things exist in Javascript, but it does in java and python) Also, since a query is a response to a request, couldn’t you define it as a poll?

A request is only a poll if the request can be handled. 404 is the response when a page cannot be found, hence more like querying than polling. It’s semantics, and not something to mull over. If you see the word poll, then assume it to mean accessing a variable for a value we know to exist.

A for callback, that is going to come up later in the topic of functions that take other functions as their arguments. Let this one go for the time.

Iteration is what we do in loops that that take in one value at a time from a set or sequence, such as a string, an array, or an object. In the case of sets, arrays and strings, those items are found by index; and, in the case of objects, those items, or entry values are found by their key.

Keep working with the loops you are learning and get in lots of practice. When it comes to the unit on iterators, you will have the necessary tools to write loops that do exactly what they do. We’re sure to come up with more questions by that time so just ask away when your reach it.

okay, consider a person. She have some properties like her hair color, height, face structure.
And then she can perform some tasks like she can speak, she can walk, she fight etc. These are the methods.
Similar is the thing in JS

1 Like

so is it safe to say properties gives information and methods modifies code, where as properties don’t modify code but tells you about a value/or attribute ?

1 Like

All methods are properties, but not all properties are methods. Properties are either data or behavior, which behavior is the methods that can operate on the data of the current instance, or owner object. Methods do not modify code. They may modify data, or simply retrieve it.

Is console.log a method or just .log? Also, could you explain why or why not?

console is an object, .log() is a method of that object.

object.property
2 Likes

console.log means that you are logging something, so that must be a method. When you are DOING something it is a method. A property, however, is kind of like a variable, it can change depending on what you wrote down. When you apply the console.log(“Hello”. length)// computer will print 4. But if you do console.log(“Bye”. length), the computer will print 3.
One thing that you do the same will be the method. Properties will change what the computer prints.

1 Like

So just out of curiosity, is .length a value that is stored somewhere for each object, or is it calculated each time someone asks for .length? How does this compare to other languages (e.g. len() in Python)?

Short Answer

.length is a property, a value that is associated with a String object. It’s not calculated every time we use .length. You can see this in how it is formatted. Properties always have the format of .propertyName while methods have the format of .methodName(). Note the parentheses at the end (just like as if we were calling a function).

Other languages’ methods of calculating and/or storing length can all be different. I encourage you to check out documentation and use Google to find this out for different languages. In terms of Python’s len(), this article is helpful (one-sentence summary: len() simply prints a value (the length) that is already stored, as it calls the .__len__() dunder method, which acts as a counter and automatically increments as the data is inputted).


Long Answer

In addition to the above, .length is calculated once, then stored as a property of the string. For string primitives (e.g. let s = "foo"), this length is not calculated right away. It is only calculated when we call .length for the first time.

This is because, according to the documentation:

In contexts where a method is to be invoked on a primitive string or a property lookup occurs, JavaScript will automatically wrap the string primitive and call the method or perform the property lookup.

Essentially, this means that when you try to access any property or method of a string, it gets converted to an Object object that is temporary, and the method or property lookup will be called on this object instead. When you try to access the .length property of the string (and if you’ve never attempted to access it before), the value is calculated using the temporary object, then stored as a property.

Rabbit hole I went down in finding this (and other useful resources):


If I missed anything, please anyone feel free to hop in!

4 Likes

properties are kind of value but methods are function.