Javascript Object properties and methods

In JavaScript, collections of data are stored in objects, each of which can have properties and methods.

Object methods are functions that are stored inside an object, whereas object attributes are variables that are kept inside an object.

Example:

// Creating an object
let car = {
    make: "Honda",
    model: "Civic",
    year: 2021,
    color: "red",
    drive: function() {
        console.log("Driving the " + this.color + " " + this.make + " " + this.model + "!");
    }
};

// Accessing object properties
console.log(car.make);  // Output: "Honda"
console.log(car.year);  // Output: 2021

// Calling object methods
car.drive();  // Output: "Driving the red Honda Civic!"

The car object in this illustration has the following four properties: make, model, year, and colour. A function that sends a message to the console is part of the drive method, which is declared as a property of the automobile object.

You can use bracket notation or dot notation to access an object’s properties. When the property name is static or not known until runtime, dot notation is used, while bracket notation is used when it is known at the time of coding.

You only need to reference an object method using dot notation and call it by appending parentheses at the end.