JavaScript objects

collections of properties and values (the "nouns")

So far, we have:

  • done some stuff with basic data values called primitives

  • assigned those primitives to locations called variables

  • done things to those variables with operators

  • done even more things to variables with functions

Now, we take it to another level with objects:

  • which encapsulate data in pairs called keys and values

    • the key acts as a sign of meaning for the value

    • the value can be a primitive, another object or even a function

const emptyObject = {}

const oneKeyObject = {
    name: "Jon"
}

const complexObject = {
    key1 : "Hello",
    key2 : "Hey", 
    anotherKey : 42, 
    someOtherKey : true, 
    nestedObject : { 
        nestedObjectKey: true
    },
    method: function () {
        return "Surprise!"
    }
}

We can gather from the above that:

  • the object has keys separated by commas

  • keys can have names similar to variables

    • but with no declaration keywords (i.e. const, let,var)

More abstractly:

keyword anObject = {
    aKey1 : aValue1, 
    aKey2 : aValue2,
    ...
}

Recall that objects copy by reference:

  • when we copy an object and we change the original...

    • ... we also make the same changes in the copy!

    • ... because variables store references to the object

      • ...and not the object itself

Last updated