JavaScript Q&A Logo
JavaScript Q&A Part of the Q&A Network

What are JavaScript objects and how are they created?

Asked on Aug 02, 2024

Answer

JavaScript objects are collections of key-value pairs, where keys are strings (or Symbols) and values can be any data type. They are used to store and manage data in a structured way.
<!-- BEGIN COPY / PASTE -->
        const person = {
            name: "Alice",
            age: 30,
            greet: function() {
                console.log("Hello, " + this.name);
            }
        };

        person.greet(); // Outputs: Hello, Alice
        <!-- END COPY / PASTE -->
Additional Comment:
  • Objects are created using curly braces {}.
  • Properties are defined as key-value pairs, separated by commas.
  • Methods can be defined as functions within the object.
  • Access properties using dot notation (e.g., person.name) or bracket notation (e.g., person["name"]).
✅ Answered with JavaScript best practices.
← Back to All Questions