Skip to content

JavaScript object properties

  • by

JavaScript objects are collections of properties, where each property is a key-value pair. Properties can be accessed using dot or bracket notation, and they can be added, deleted, or modified at runtime.

const objectName = {
  propertyName1: propertyValue1,
  propertyName2: propertyValue2,
  ...
};

Here’s an example of an object with some properties:

const person = {
  name: "John",
  age: 30,
  gender: "male"
};

You can access the properties of an object using dot notation or bracket notation:

console.log(person.name); // "John"
console.log(person["age"]); // 30

You can also add, delete, or modify properties of an object at runtime:

person.city = "New York"; // add a new property
delete person.gender; // delete a property
person.age = 31; // modify an existing property

JavaScript object properties example

Simple example code of how you can use JavaScript object properties to create a simple program that stores information about books in a library:

const library = {
  books: [
    {
      title: "The Great Gatsby",
      author: "F. Scott Fitzgerald",
      pages: 180,
      read: false
    },
    {
      title: "To Kill a Mockingbird",
      author: "Harper Lee",
      pages: 281,
      read: true
    },
    {
      title: "1984",
      author: "George Orwell",
      pages: 328,
      read: false
    }
  ],
  addBook: function(title, author, pages, read) {
    this.books.push({
      title: title,
      author: author,
      pages: pages,
      read: read
    });
  },
  getBookInfo: function(index) {
    return `${this.books[index].title} by ${this.books[index].author}, ${this.books[index].pages} pages, ${this.books[index].read ? "read" : "not read yet"}`;
  }
};

// add a new book
library.addBook("The Catcher in the Rye", "J.D. Salinger", 224, false);

// get first book in the library
console.log(library.getBookInfo(0));

Output:

JavaScript object properties

JavaScript for…in Loop object properties

The for...in loop in JavaScript is used to iterate over the properties of an object. It allows you to loop through all the enumerable properties of an object, including any properties that may have been added dynamically at runtime.

Here is the syntax for using the for...in loop:

for (property in object) {
  // do something with each property
}

Here’s an example of how to use the for...in loop to loop over the properties of an object:

const person = {
  name: "John",
  age: 30,
  gender: "male"
};

for (let prop in person) {
  console.log(prop + ": " + person[prop]);
}

In this example, the for...in loop iterates over the properties of the person object, and for each property, it logs the property name and its corresponding value to the console.

Comment if you have any doubts or suggestions on this JS Object topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *