JavaScript Tutorials

Overview

JavaScript object properties can be changed, added, deleted, and mad read only. Object methods are actions that can be performed on objects.

Creating an Object

const car = {
    type: "Maserati", 
    model: "Quattroporte", 
    color: "white",
    fullName: function() {
        return this.type + " " + this.model; // this is a method stored as a property/value pair
    }
};

Accessing Object Properties

There are two ways to access the properties of an JavaScript object.

let myCar = car.type; // Maserati
let myCar = car["type"]; // Maserati

Adding a New Property to the Object

A new property can be added to an existing object by naming it and giving it a value.

car.year = 2025;
car["year"] = 2025;

Deleting an Object Property

The delete keyword is used to delete a property from an object.

delete car.year;
delete car["year"];

Note

The delete keyword deletes both the value of the property and the property itself.

Nesting Objects

The delete keyword is used to delete a property from an object.

const car = {
    type: "Maserati", 
    model: "Quattroporte", 
    color: "white",
        myCars: {
            car1: "Maserati",
            car2: "Alfa Romeo"
        }
};

Adding a Method to an Object

Object methods are actions that can be performed on objects. They are a function definition stored as a property value.

const car = {
    type: "Maserati", 
    model: "Quattroporte", 
    color: "white",
    fullName: function() {
        return this.type + " " + this.model; // this is a method stored as a property/value pair
    }
};

Accessing an Object Method

The following syntax is used to access a method.

objectName.methodName()

The following accesses the function fullName stored inside the car object.

carName = car.fullName(); // Maserati Quattroporte

If the fullName property is accessed without (), it will return the function definition.

function() { return this.type + " " + this.model; }

JavaScript Notes:

  • When using JavaScript, single or double quotation marks are acceptable and work identically to one another; choose whichever you prefer, and stay consistent
  • JavaScript is a case-sensitive language; firstName is NOT the same as firstname
  • JavaScript variables are case sensitive (x is not the same as X)
  • Arrays count starting from zero NOT one; so item 1 is position [0], item 2 is position [1], and item 3 is position [2] … and so on
  • JavaScript variables must begin with a letter, $, or _

We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.