JavaScript Object Display

JavaScript Object Display – Show Objects in HTML

In JavaScript, an object is a collection of properties, and a property is an association between a name (or key) and a value. Objects can be used to store various types of data and are a fundamental part of the language.


Syntax of an Object

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let person = {
firstName: "John",
lastName: "Doe",
age: 30,
profession: "Engineer"
};
let person = { firstName: "John", lastName: "Doe", age: 30, profession: "Engineer" };
let person = {
  firstName: "John",
  lastName: "Doe",
  age: 30,
  profession: "Engineer"
};

Try It Now

In the above example, person is an object with four properties: firstName, lastName, age, and profession.

Accessing Object Properties

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
console.log(person.firstName); // Output: John
console.log(person["age"]); // Output: 30
console.log(person.firstName); // Output: John console.log(person["age"]); // Output: 30
console.log(person.firstName); // Output: John
console.log(person["age"]);    // Output: 30

Try It Now

Modifying Object Properties

You can modify the properties of an object like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
person.age = 31;
person["profession"] = "Software Developer";
console.log(person.age); // Output: 31
console.log(person.profession); // Output: Software Developer
person.age = 31; person["profession"] = "Software Developer"; console.log(person.age); // Output: 31 console.log(person.profession); // Output: Software Developer
person.age = 31;
person["profession"] = "Software Developer";

console.log(person.age); // Output: 31
console.log(person.profession); // Output: Software Developer

Try It Now

Adding New Properties

You can add new properties to an object:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
person.country = "USA";
console.log(person.country); // Output: USA
person.country = "USA"; console.log(person.country); // Output: USA
person.country = "USA";
console.log(person.country); // Output: USA

Try It Now