What is the difference between a property and a method, and when do I need to use parenthesis?

I didn’t really understand the difference between properties and methods, could somebody please explain that to me?

Properties of an object can be either a value, or a method (a function only accessible to an instance of the object).

obj = {
    prop: 'value',
    method () {
        return this.prop
    }
}

obj.prop      //  'value'

obj.method()  //  'value'
7 Likes

Not really happy with the way this exercise justifies the use of the () that appends methods such as console.log(), as it pertains to *.length.

If .length is a method(as the exercise claims) then it too should be appended by (). I’ve read elsewhere that .length is not a method but rather a property.

So which is it? A property or a method? And why claim it’s one thing and not the other?

1 Like

A property of all iterables.

object.length

Object has a length property but it is always 1, if I’m not mistaken.

 > Object.hasOwnProperty('length')
<- true
 > Object.length
<- 1
 >

Iterables include arrays, sets and strings.

https://javascript.info/iterable

3 Likes

thanks. that contradicts the information in the exercise, but that helps clarify things

1 Like

What is the main difference between Javascript methods (like toUpperCase() and trim()) and properties (like length in previous exercise)? It seems that they have similar functionality.

1 Like

Similar notation, but different functionality. All objects have a length property, though some of them are fixed at zero (not iterable). Only strings have a .toLowerCase() and .trim() method.

This breaks down to two levels, the top level, Object and the secondary level, String. .length is an attribute of the Object class, and since that is the top of the prototype chain, all subclasses can reach up the chain to the length attribute.

Attributes that are values have no functionality, but those that are methods do have it. Be sure to reach out to MDN for a greater understanding of String methods, Object.prototype, and other JS concepts.

1 Like

Why do I have to put () after a method in JavaScript.

That is in keeping with function syntax. All functions (methods of an object) require a parameter container, even if there are no parameters.

const foo = () => {}

const foo = function () {}

function foo () {}

object = {
    foo () {

    }
}
2 Likes

Why built-in string method like .toUpperCase() foresee empty round brackets in their syntax?

When we write parentheses at the end of an identifier (name, variable, etc.) it tells the interpreter to invoke a function by that name, if it exists.

.toUpperCase is a method (a function written in a class) so if we wish to invoke it, we need to include (). This applies to all functions/methods.

1 Like

Thanks @mtf as always your answer are the best :slight_smile:.

Could you please explain me what’s an object in Javascript and what does it mean that some of that, like Math, are built-in?

Thanks.

1 Like

Everything in JavaScript is an object, starting with the window object and on down. It’s a bit tricky to understand, at first, and therefore tricky to explain.

An object is a sort of enclosure wherein are contained properties, some are property-value pairs, and some are property-method pairs. Methods are akin to functions, but they are dedicated to, and can only be invoked from within the confines of the object enclosure.

ball = {
    type: hardball,
    diameter: [73, 74, 75, 76],
    diameter_unit: 'centimeter',
    weight: [142, 143, 144, 145, 146, 147, 148, 149],
    weight_unit: 'grams'
}

The above is an object literal. Note the curly braces that delimit the object properties? This is the enclosure we spoke of earlier. Note the property-value pairs?

prop: value

It follows that there are all kinds of balls that we use in sport. You’ll learn later how we can define a blueprint that all of them can follow in describing themselves. Look for this in the Classes unit.

Let’s add a method to this object…

ball = {
        type: fastball,
        diameter: [73, 74, 75, 76],
        diameter_unit: 'centimeter',
        weight: [142, 143, 144, 145, 146, 147, 148, 149],
        weight_unit: 'grams',
        pitch_dimensions: 'kph',
        pitch () {
            return Math.random() * (102 - 80) + 80
        }
    }
console.log(ball.pitch())    // will return a random float between 80 and 102 kilometers per hour.

Note above that we used a method of the built in Math object. Built in objects are known as Special Objects. In JS that would include,

Math
Date
RegExp

among others. This will begin to make more sense as you progress through the course.

7 Likes

Thanks @mtf this is very clear to me at the moment :slight_smile: Thanks for your explanation

2 Likes

You’re welcome. Note that I edited the example to generate values in the correct range.

3 Likes

Properties are information providers while Methods are actionable functions.

Properties let you have information about a particular instance of a data type. e.g ‘Hello’…which is a string. to know about the length information about this particular string which is …‘hello’…i will add a string property .length to it.
Therefore ‘hello’.length //Prints 5. therefore properties of datatypes provides information or tells us their characteristics of the particular instance.

Methods are actually functions and functions do things, they make things happen …this is why they are actionable. when you call functions, there must be () at the end to show it is a function and for it to run. e.g .toUpperCase(), .log()
so , if we have an instance of a string “hello”…to add an action to it, we use a method
example ‘hello’.toUpperCase() //prints HELLO.
notice that the method caused a change in appearance …which is the action.
so Methods make things to happen, properties provide information about the instance of that datatype.

17 Likes

This is probably old and maybe its been updated since then but for my lesson, .length is a property, methods are preceded by a . and followed by a set of parentheses.

1 Like

If Properties are information providers while Methods are actionable functions why is Number.isInteger() considered as a static method? It just provides an information about if a number is an integer or not.

Because it can only be called on the class, directly and requires an argument.

42.isInteger()          // error

Number.isInteger(42)    // true
2 Likes

Actually it doesn’t. The exercise calls “.length” a property. At all times. The word “method” is not used anywhere. Only on the next exercise, of course :slight_smile: