Last modified: Jul 20, 2026

JavaScript Object Variables Guide

JavaScript object variables are a core part of the language. They store collections of data and more complex entities. Think of an object as a container. This container holds key-value pairs. Each key is a property name. Each value can be any data type, including functions.

This guide is for beginners. It will explain how to create, use, and manage JavaScript object variables. You will learn about properties, methods, and common operations. We will use clear examples and short explanations.

What is a JavaScript Object Variable?

An object variable is a variable that holds a reference to an object. Unlike primitive types like numbers or strings, objects are mutable. You can change their properties after creation. This makes them very powerful for organizing code.

Objects help you group related data. For example, a person object can have properties for name, age, and city. This is much cleaner than using separate variables.

Creating JavaScript Object Variables

There are two common ways to create an object. The first is using an object literal. The second is using the new Object() syntax. The object literal is simpler and more common.

 
// Using object literal
let car = {
  brand: "Toyota",
  model: "Corolla",
  year: 2020
};

// Using new Object() syntax
let book = new Object();
book.title = "JavaScript Guide";
book.author = "John Doe";

In the first example, we create a car object. It has three properties: brand, model, and year. In the second example, we start with an empty object. Then we add properties one by one.

Accessing Object Properties

You can access properties using dot notation or bracket notation. Dot notation is easier to read. Bracket notation is useful when the property name is dynamic or contains special characters.

 
let person = {
  name: "Alice",
  "favorite color": "blue"
};

// Dot notation
console.log(person.name); // Output: Alice

// Bracket notation
console.log(person["favorite color"]); // Output: blue

Dot notation is clean. But it only works with valid variable names. The property "favorite color" has a space. So we must use bracket notation.

Modifying Object Properties

Objects are mutable. You can change property values at any time. You can also add new properties or delete existing ones.

 
let user = {
  username: "coder123",
  email: "[email protected]"
};

// Change a property
user.email = "[email protected]";

// Add a new property
user.age = 25;

// Delete a property
delete user.email;

console.log(user);
// Output: { username: 'coder123', age: 25 }

Notice how we changed the email. Then we added the age property. Finally, we deleted the email property. The object now has only two properties.

Working with Object Methods

A method is a function stored as a property. Objects can have methods to perform actions. This is a key feature of object-oriented programming in JavaScript.

 
let calculator = {
  add: function(a, b) {
    return a + b;
  },
  subtract: function(a, b) {
    return a - b;
  }
};

console.log(calculator.add(5, 3)); // Output: 8
console.log(calculator.subtract(10, 4)); // Output: 6

In this example, the calculator object has two methods. Each method takes two arguments and returns a result. This keeps related functions together.

Using the this Keyword in Objects

Inside a method, the this keyword refers to the object itself. This allows methods to access other properties of the same object.

 
let person = {
  firstName: "Bob",
  lastName: "Smith",
  fullName: function() {
    return this.firstName + " " + this.lastName;
  }
};

console.log(person.fullName()); // Output: Bob Smith

The fullName method uses this.firstName and this.lastName. It accesses the object's own properties. This is very useful for building complex behavior.

Nested Objects and Arrays

Object properties can be other objects or arrays. This allows you to create complex data structures. For example, a company object can have an employees array.

 
let company = {
  name: "Tech Corp",
  location: "New York",
  employees: [
    { name: "Alice", role: "Developer" },
    { name: "Bob", role: "Designer" }
  ],
  founder: {
    name: "Charlie",
    age: 45
  }
};

console.log(company.employees[0].name); // Output: Alice
console.log(company.founder.age); // Output: 45

Accessing nested data requires chaining dots or brackets. This example shows how to get the first employee's name and the founder's age.

Iterating Over Object Properties

You can loop through all properties of an object. The for...in loop is perfect for this. It gives you each property name one by one.

 
let scores = {
  math: 90,
  science: 85,
  english: 88
};

for (let subject in scores) {
  console.log(subject + ": " + scores[subject]);
}
// Output:
// math: 90
// science: 85
// english: 88

This loop prints each subject and its score. The variable subject holds the property name. We use bracket notation to get the value.

Checking if a Property Exists

Sometimes you need to check if a property exists in an object. Use the hasOwnProperty method or the in operator. This helps avoid errors.

 
let car = {
  brand: "Honda",
  model: "Civic"
};

console.log(car.hasOwnProperty("brand")); // Output: true
console.log("model" in car); // Output: true
console.log(car.hasOwnProperty("year")); // Output: false

Both methods work well. hasOwnProperty only checks the object itself. The in operator also checks the prototype chain.

Copying Objects

Objects are copied by reference, not by value. This means two variables can point to the same object. To make a true copy, you need to clone the object. One simple way is using Object.assign.

 
let original = { a: 1, b: 2 };
let copy = Object.assign({}, original);

copy.a = 99;

console.log(original.a); // Output: 1
console.log(copy.a); // Output: 99

Now original and copy are separate objects. Changing one does not affect the other. This is called a shallow copy.

Common Mistakes with Object Variables

Beginners often make mistakes with object variables. One common error is trying to access a property that does not exist. This returns undefined. Another mistake is forgetting that objects are mutable.

For example, if you assign an object to a new variable, both point to the same data. Changing one changes both. This is different from primitive types.

 
let obj1 = { value: 10 };
let obj2 = obj1;

obj2.value = 20;

console.log(obj1.value); // Output: 20

This can cause unexpected bugs. Always remember that object variables hold references, not the actual data.

Object Destructuring

Destructuring is a modern way to extract properties from an object. It makes your code cleaner and more readable. You can assign properties to variables in one line.

 
let user = {
  name: "Diana",
  age: 30,
  city: "London"
};

let { name, age } = user;

console.log(name); // Output: Diana
console.log(age); // Output: 30

The variables name and age now hold the values from the object. This is very useful when working with function parameters.

For more on variable behavior, check our JavaScript Variable Scope Explained guide.

Object Methods for Manipulation

JavaScript provides built-in methods to work with objects. Object.keys() returns an array of property names. Object.values() returns an array of values. Object.entries() returns both.

 
let fruit = {
  name: "Apple",
  color: "Red",
  weight: 200
};

let keys = Object.keys(fruit);
let values = Object.values(fruit);
let entries = Object.entries(fruit);

console.log(keys); // Output: ['name', 'color', 'weight']
console.log(values); // Output: ['Apple', 'Red', 200]
console.log(entries); // Output: [['name', 'Apple'], ['color', 'Red'], ['weight', 200]]

These methods are very useful for iterating or transforming data. They work on any object.

Conclusion

JavaScript object variables are powerful and flexible. They help you organize data and code. You learned how to create, access, and modify objects. You also saw how to use methods, nested structures, and destructuring.

Practice is key. Try creating your own objects. Experiment with different properties and methods. This will build your confidence. For more on variable types, see our JavaScript Variable Types Guide.

Understanding objects is a big step in mastering JavaScript. Keep coding and exploring. You can also review Types of JavaScript Variables for a broader view.