</> Code Corner

JavaScript · JS Tutorial · Lesson 12

JS Objects

Properties, methods and the this keyword.

Objects are containers for named values called properties. Functions stored in objects are called methods.

Example

<p id="demo"></p>
<script>
const person = {
  firstName: "John",
  lastName: "Doe",
  age: 50,
  fullName: function () {
    return this.firstName + " " + this.lastName;
  }
};
person.eyeColor = "blue";       // add a property
document.getElementById("demo").innerHTML =
  person.fullName() + " is " + person["age"] + " with " + person.eyeColor + " eyes.";
</script>
  • Access properties with object.property or object["property"]
  • Inside a method, this refers to the object
  • Objects are written with curly braces and name:value pairs

Keep reading “JS Objects”

Sign up to read the full lesson, try every example in the live editor, and take the 5-question quiz.

Chat with us 👋