JavaScript Objects Explained

In JavaScript, objects are used to store multiple values in a single variable.

Think of an object like a real-world item, such as a "person." A person has properties like name, age, and maybe a method like getFullName() that tells you the full name.

Unlike primitive types (like numbers or strings), objects are non-primitive and can hold many values using key-value pairs.


How to Create Objects

1. Using Object Literal (Easiest Way)

This is the short and simple way to create an object.

    var person = {
    firstName: "James",
    lastName: "Bond",
    age: 25,
    getFullName: function() {
        return this.firstName + " " + this.lastName;
    }
    };

2. Using Object Constructor

This is the longer way using new Object().

    var person = new Object();
    person.firstName = "James";
    person.lastName = "Bond";
    person.age = 25;
    person.getFullName = function() {
    return this.firstName + " " + this.lastName;
    };

Accessing Properties and Methods

You can access object values in two ways:

    person.firstName;        // James (dot notation)
    person["lastName"];      // Bond (bracket notation)

    person.getFullName();    // "James Bond" (method call)

If you call a method without parentheses (like person.getFullName), it will just return the function code, not the result.


 Missing Properties Return undefined

    var person = {};
    console.log(person.firstName); // undefined

To check if a property exists, use:

    if (person.hasOwnProperty("firstName")) {
    console.log(person.firstName);
    }

Each Object Is Separate

Each object is independent. If you change one, the other doesn’t change — unless they point to the same memory.

    var p1 = new Object();
    p1.firstName = "James";

    var p2 = new Object();
    console.log(p2.firstName); // undefined

    var p3 = p1; // Now p3 and p1 point to the same object
    p3.firstName = "Steve";

    console.log(p1.firstName); // Steve

Loop Through Object Properties

Use a for...in loop to go through each property in the object.

    var person = {
    firstName: "James",
    lastName: "Bond"
    };

    for (var key in person) {
    console.log(key);         // firstName, lastName
    console.log(person[key]); // James, Bond
    }

Pass by Reference

Objects in JavaScript are passed by reference, not by value.

    function changeName(obj) {
    obj.firstName = "Steve";
    }

    var person = { firstName: "Bill" };
    changeName(person);

    console.log(person.firstName); // Steve

 Nested Objects

You can have objects inside other objects.

    var person = {
    firstName: "James",
    address: {
        city: "London",
        country: "UK"
    }
    };

    console.log(person.address.country); // UK

 Common Mistakes

  • Don’t declare a property without a value:

    var person = { firstName };     // ? Error
    var person = { age: };          // ? Error

Summary Table

TopicExample
Object Literalvar obj = { key: value };
Object Constructorvar obj = new Object();
Add Propertyobj.name = "John";
Add Methodobj.sayHi = function() { ... };
Access Propertyobj.name or obj["name"]
Access Methodobj.sayHi()
Check Property Existsobj.hasOwnProperty("name")
Loop Through Propertiesfor (var key in obj)
Nested Objectobj.address.city
Pass by ReferenceChanges in function affect original object