Objects

Objects

  • Objects are used to represent real world objects in programming

  • Within an object, variables are known as properties and functions are known as methods

  • Curly braces are a means of identifying if a variable is an object


// create an object that represents a dog

let myDog = {
  name: "Fido",
  age: 4,
  speak: function() {
    console.log('Woof woof');
  }
}

  • In the example above, we created an object and stored it in a variable called myDog

  • This object has two (2) properties (name and age) and 1 method (speak)


Accessing Properties and Methods of Objects

  • Properties and values can be assigned and read using Dot notation

let myDog = {
  name: "Fido",
  age: 4,
  speak: function() {
    console.log('Woof woof');
  }
}

// store myDog's name in a variable called dogName
let dogName = myDog.name;

// call the speak method of myDog
myDog.speak();

JS Bin on jsbin.com


Updating Object properties using dot notation

  • Object properties can be updated, added or deleted

JS Bin on jsbin.com

Think of methods as actions that our objects can take. Objects are another flexible and powerful feature of Javascript. Click here for more information about Objects