Lesson 14 of 30 – JavaScript Objects
37%

JavaScript Objects

A JavaScript object is a non-primitive data type that stores multiple values in the form of properties and methods.

Note: Objects help organize related data and functions into a single unit.
What is an Object?

Objects are collections of key-value pairs.

  • Store multiple values.
  • Can contain properties and methods.
  • Can represent real-world entities.
  • Can be created in different ways.
Example:
var person = {
name:"Ritesh",
age:25
};
Ways to Create Objects

JavaScript provides two common ways to create objects.

1. Object Literal
var p1 = {name:"Ritesh"};
2. Object Constructor
var p2 = new Object();
p2.name="Ritesh";

Both methods create similar objects.

Object Literal Syntax

Object literals are the easiest and most commonly used way to create objects.

Syntax:
var objectName={ key1:value1, key2:value2 };
Example:
var person={ firstName:"Ritesh", lastName:"Kumar", age:25 };
Objects with Methods

Objects can also contain functions called methods.

var person={ firstName:"Ritesh", lastName:"Kumar", getFullName:function(){ return this.firstName+" "+this.lastName; } };

Methods perform actions using object properties.

Invalid Object Syntax

Every property must have a value.

Wrong Examples:
var person={firstName};

var person={getFullName;};

Always use key:value pairs.

Create Objects using Object() Constructor

Objects can also be created using the Object() constructor and the new keyword.

Example:
var person = new Object();

person.firstName = "Ritesh";
person["lastName"] = "Kumar";
person.age = 35;

person.getFullName = function(){
return this.firstName + " " + this.lastName;
};

Properties can be added using dot notation or square brackets.

Variables as Object Properties

Variables can also be used as object properties.

Example:
var firstName = "Ritesh";
var lastName = "Kumar";

var person = {
firstName,
lastName
};

JavaScript automatically creates properties with the same variable names.

Access Object Properties

Object properties can be accessed in two ways.

  • Dot notation
  • Square bracket notation
Example:
var person={
firstName:"Raushan",
lastName:"Kumar"
};

person.firstName;
person["lastName"];

Both methods return the property value.

Access Object Methods

Methods are functions stored inside objects.

Example:
var person={
firstName:"Raushan",
lastName:"Kumar",

getFullName:function(){
return this.firstName+" "+this.lastName;
}
};

person.getFullName();

Methods are called using parentheses ().

hasOwnProperty() Method

Use hasOwnProperty() to check whether a property exists.

Example:
var person = new Object();

if(person.hasOwnProperty("firstName")){
document.write(person.firstName);
}

If the property does not exist, JavaScript returns undefined.

Object Assignment

Objects are assigned by reference.

Example:
var p1 = new Object();
p1.firstName="Raushan";
p1.lastName="Kumar";

var p2 = p1;

p2.firstName="Sachin";
p2.lastName="Tendulkar";

Changing p2 also changes p1 because both refer to the same object.

Enumerating Object Properties

Use the for...in loop to access all properties of an object.

Example:
var person = {
firstName:"Raushan",
lastName:"Kumar"
};

for(var prop in person){
document.write(prop);
document.write(person[prop]);
}

The loop accesses both property names and their values.

Pass by Reference

Objects are passed by reference in JavaScript.

Example:
function changeName(per){
per.firstName="Raushan";
}

var person={firstName:"Raju"};

changeName(person);

The original object changes because the function receives its reference.

Nested Objects

An object can contain another object.

Example:
var person={
firstName:"Raushan",
lastName:"Kumar",
address:{
id:1,
country:"India"
}
};

person.address.country;

Nested objects help organize complex data.

Advantages of Objects
  • Store multiple related values.
  • Improve code organization.
  • Can contain methods.
  • Support nested structures.
  • Useful for real-world data modeling.
  • Easy to extend with new properties.
Key Points
  • Objects are non-primitive data types.
  • They store properties and methods.
  • Objects can be created using literals or constructors.
  • Properties can be accessed using dot or bracket notation.
  • Methods are functions inside objects.
  • Objects are assigned and passed by reference.
  • Nested objects are supported.
  • for...in is used to iterate over properties.
Quick Revision
Concept Example
Create Object { }
Constructor new Object()
Access Property obj.name
Access Method obj.method()
Check Property hasOwnProperty()
Loop for...in
Best Practices
  • Use object literals whenever possible.
  • Choose meaningful property names.
  • Keep objects organized.
  • Use methods for related actions.
  • Check properties before accessing them.
  • Avoid unnecessary nested structures.

🧠 Quick Quiz

Which syntax is used to create an object literal in JavaScript?