What is an Object in Programming? (Simplest Explanation with Examples)

“Just give me the user’s name, age, and city.”

Simple request — but in code, storing three pieces of information about one person already starts getting complicated. Now imagine doing that for thousands of users.

That is exactly the problem objects solve. In this guide, you will learn what objects are, how they work, and why every real application in the world depends on them — explained simply, with examples that make the concept stick.

core concept

What is an object
in coding?

A smarter way to store data — where everything related lives together under one name, clearly labeled and instantly accessible.

simple definition

An object groups related data under one name

An object is a collection of related data stored together with labels. Instead of five separate variables for one person, you get one object that holds all five — each piece clearly named with a key and a value.

The first time I used an object, I realized I had been making my code ten times harder than it needed to be. One object replaced a dozen loose variables instantly.

object key value property method
real-life analogy

The ID card, car & phone

You already use objects in real life — you just didn’t know the name. Every one of these things has labeled properties that belong to it. That’s exactly how objects work in code.

📄
ID Card
name: “Sara”
age: 25
city: “Lahore”
🚗
Car
brand: “Toyota”
color: “red”
speed: 120
📱
Phone
brand: “Apple”
storage: “128GB”
color: “black”
comparison

Objects vs variables — why objects win

Variables work for one value. Objects work for everything that belongs together. See how messy separate variables get — and how one object fixes it all:

loose variables
name = "Sara"
age = 25
city = "Lahore"
email = "s (at) mail.com"
# 4 vars, no connection
Messy. No structure.
one object
user = {
  "name": "Sara",
  "age": 25,
  "city": "Lahore"
} # one place ✓
Clean. Connected. Easy.
why objects

Why do we
need objects?

Without objects, storing even basic data about one person becomes a mess. Here’s the problem — and how objects fix everything.

problem Storing data without objects

Without objects, you need one variable per piece of data. For one person — name, age, city, email — that’s four separate variables with no connection.

Now imagine 50 users. That’s 200 loose variables floating around. Finding, updating, or passing them around becomes a nightmare.

no_object.py
name  = "Sara"
age   = 25
city  = "Lahore"
email = "s (at) mail.com"
# 50 users = 200 vars 😩
problem →no connection, hard to manage
✓ solution

How one object organizes everything

One object holds all the related data together — labeled, organized, instantly accessible. All four pieces of user data now live in one place with clear names.

user = {
  "name":  "Sara",
  "age":   25,
  "city":  "Lahore",
  "email": "s (at) mail.com"
}
# one object, all data, clean ✓
real world

Real-world use cases of objects

Every app you use stores its data in objects. Here’s what’s running behind the scenes:

👤
User profiles
name, email, age — all in one user object
🛍
Products
title, price, stock — one product object
👥
Game characters
health, score, level — one player object
📦
Orders
items, total, address — one order object
two parts

Properties and methods —
the two parts of every object

Every object in coding is made of exactly two things. Once you know these, reading and writing objects becomes second nature.

properties

What are properties?

A property is a piece of data stored inside an object. It has a label (the key) and a value attached to it. Think of it as “what the object knows.”

  • name: “Sara”
  • age: 25
  • city: “Lahore”
methods

What are methods?

A method is a function stored inside an object. It defines an action the object can perform. Think of it as “what the object does.”

  • greet()
  • login()
  • logout()
easy way to remember

Properties vs methods — easy way to remember

The simplest trick: properties = IS, methods = DOES. A person IS named Sara and IS 25 years old. A person DOES greet and DOES login. That’s all there is to it.

properties = IS
What the object knows
user.name → “Sara”
user.age → 25
methods = DOES
What the object can do
user.greet() → “Hi!”
user.login() → true
Memory trick: Properties store data — they have a value sitting there. Methods perform actions — they always have ( ) after the name.
create

How to create
an object

Creating an object takes one line. Curly braces, key-value pairs, done.

JavaScript

Object syntax in JavaScript

In JavaScript, an object uses curly braces with key-value pairs separated by colons. Keys are unquoted. Values can be strings, numbers, or anything else.

object.js
let user = {
  name: "Sara",
  age:  25,
  city: "Lahore"
};

// access a property
console.log(user.name); // "Sara"
output →“Sara”
Python

Object syntax in Python

Python uses dictionaries — the same curly brace idea, but keys must be in quotes. Access with bracket notation: user["name"].

object.py
user = {
  "name": "Sara",
  "age":  25,
  "city": "Lahore"
}

# access a property
print(user["name"]) # "Sara"
output →Sara
empty vs filled

Empty object vs pre-filled object

Start empty and add later, or fill it right away — both work perfectly in real projects.

empty object
user = {}

# add later
user["name"] = "Sara"
user["age"]  = 25
Start empty, fill as you go
pre-filled
user = {
  "name": "Sara",
  "age":  25,
  "city": "Lahore"
}
All values ready from start ✓
access

How to access
object data

Getting data out of an object is step two. There are two ways — and both are simple once you see them side by side.

dot notation

Dot notation — the simple way

Put a dot after the object name, then write the key directly. Clean, short, and the most common way beginners use. Works in both JS and Python (for class attributes).

object.key → value
user.name # “Sara” user.age # 25 user.city # “Lahore”
bracket notation

Bracket notation — the flexible way

Wrap the key in square brackets and quotes. More flexible — works when the key has spaces, special characters, or is stored in a variable.

object[“key”] → value
user[“name”] # “Sara” user[“age”] # 25 user[“city”] # “Lahore”
when to use

When to use dot vs bracket notation

Both do the same job. Choose based on your situation:

situationuse dotuse bracket
Simple key nameuser.name defaultuser[“name”]
Key has spaces✗ won’t workuser[“first name”] use this
Key stored in variable✗ won’t workuser[myKey] use this
Python (dict)✗ not for dictsuser[“name”] always
Default rule: Use dot notation in JavaScript — it’s cleaner. Switch to bracket notation when the key has spaces, uses special characters, or you’re writing Python.
modify

Adding, updating and
deleting properties

Objects aren’t locked after you create them. Add new data, change existing values, or remove what you no longer need — anytime.

➕ add How to add a new property

Assign a value to a key that doesn’t exist yet. The object creates it on the spot — no setup needed.

user.email = “s (at) mail.com”
add.py
user = {"name": "Sara"}

# add new property
user["email"] = "s (at) mail.com"

print(user)
result →{“name”:”Sara”,”email”:”s (at) mail.com”}
✎️ update How to update an existing property

Assign a new value to a key that already exists. It simply overwrites the old one — same syntax as adding, just an existing key.

user.age = 26
update.py
user = {"age": 25}

# update existing value
user["age"] = 26

print(user["age"])
result →26 (was 25)
🗑️ delete How to delete a property

Use del in Python or delete in JavaScript before the property — it removes the key and its value completely from the object.

del user[“city”]
delete.py
user = {
  "name": "Sara",
  "city": "Lahore"
}
# remove city
del user["city"]
result →{“name”: “Sara”} — city gone
methods

Methods — functions
inside objects

Properties store data. Methods perform actions. Together, they make objects complete and powerful.

definition

What is a method in an object?

A method is a function stored inside an object. Just like properties have a key and a value — a method has a key and a function as its value. You call it using the object name, a dot, and the method name with parentheses.

name: “Sara” ← property    greet() ← method
this keyword

The “this” keyword — what it means

Inside a method, this refers to the object itself. It lets the method access the object’s own properties without knowing the variable name. Think of it as the object pointing to itself.

object
user = {
  name: “Sara”,
  greet: fn…
}
→
this = user
this.name
= “Sara”

same thing
JavaScript

Method example in JavaScript

method.js
let user = {
  name: "Sara",
  age:  25,
  greet: function() {
    return "Hi, I'm " + this.name;
  }                // ↑ this = user
};

console.log(user.greet());
output →Hi, I’m Sara
Python

Method example in Python

method.py
class User:
    def __init__(self, name):
        self.name = name   # self = this

    def greet(self):
        return f"Hi, I'm {self.name}"

sara = User("Sara")
print(sara.greet())
output →Hi, I’m Sara
nested

Nested objects —
objects inside objects

Objects can hold other objects as values. This is how real-world data is actually structured in every app you use.

definition

What is a nested object?

A nested object is when one object contains another object as a value. Instead of a simple string or number for a key, the value itself is a full object with its own keys and values.

user = { “name”: “Sara”, “address”: { object inside “city”: “Lahore”, “country”: “Pakistan” } }
access

How to access nested object data

Chain the keys together with dots — go into the outer object first, then into the inner one. Each dot takes you one level deeper.

user . address . city → “Lahore”
user . address . country → “Pakistan”
real world

Real-world example — user with address

Every user profile in a real app has nested objects — address, preferences, account settings. Here’s what it looks like in Python:

nested.py
user = {
  "name": "Sara",
  "age":  25,
  "address": {
    "city":    "Lahore",
    "country": "Pakistan"
  }
}

print(user["address"]["city"])
output →Lahore
accessing nested values
user[“name”] → “Sara”
user[“address”][“city”] → “Lahore”
user[“address”][“country”] → “Pakistan”
Object.operations()

Common object operations
every beginner should know

These three operations are the ones you’ll use constantly in real projects. Learn them once and they’ll feel natural everywhere.

keys & values

Object.keys() and Object.values()

These built-in methods give you all keys or all values from an object as a list — making it easy to loop through, display, or check what’s inside.

Object.keys()
Object.keys(user)

// returns:
["name",
 "age",
 "city"]
→ list of all keys
Object.values()
Object.values(user)

// returns:
["Sara",
 25,
 "Lahore"]
→ list of all values
looping

Looping through an object

Use for…in to loop through every key in an object. On each pass, you get the key — and you can use it to grab the value too.

loop.js
const user = { name: "Sara", age: 25 };

for (let key in user) {
  console.log(key + ": " + user[key]);
}
// name: Sara
// age: 25
output →name: Sara   age: 25
destructuring

Object destructuring — the modern way

Destructuring lets you pull multiple values out of an object in one line — much cleaner than writing separate assignments for each property.

✗ old way
let name = user.name;
let age  = user.age;
let city = user.city;
// 3 separate lines
✓ destructuring
let { name, age,
      city } = user;
// one line ✓
// same result
result →name=”Sara”   age=25   city=”Lahore”
arrays vs objects

Objects vs arrays —
what’s the difference?

Both store multiple values — but they do it differently. Knowing which to choose makes your code cleaner and smarter.

storage

How they store data differently

An array uses numbered indexes — items have a position (0, 1, 2). An object uses named keys — items have a label. Arrays are ordered lists. Objects are labeled collections.

[ ] array — numbered
[0]
Ali
[1]
Sara
[2]
Hamza
{ } object — labeled
name:“Sara”
age:25
city:“Lahore”
when to use

When to use object vs array

Ask yourself one question: am I storing a list of similar items — or describing one thing with different details?

use array when
  • You have a list of similar items
  • Order matters
  • You need to loop through all
use object when
  • Items have different labels
  • You describe one specific thing
  • You access by name, not position
feature[ ] array{ } object
Access byIndex [0]Key “name”
Best forLists of itemsOne thing, many details
OrderAlways preservedNot guaranteed
Example[“Ali”,”Sara”]{name:”Sara”,age:25}
Loop withfor…of / forEachfor…in / Object.keys
OOP basics

What is a class?
(objects come from classes)

A class is where objects come from. Once you understand this, object-oriented programming starts to make complete sense.

blueprint vs thing

Class vs object — blueprint vs thing

A class is a blueprint — it defines what an object should look like. An object is the actual thing built from that blueprint. You write the class once, then create as many objects from it as you need.

Think of a class like a cookie cutter — the object is the actual cookie. Same shape, but each one has its own data.

📄 class — blueprint
Written once
Defines structure — no real data yet
class User: …
→
📦 objects — real things
Created many times
Each has its own actual data
sara = User(“Sara”)
ali = User(“Ali”)
JavaScript

In JavaScript, use the class keyword to define the blueprint. Then create objects with new ClassName(). The constructor runs automatically when the object is created.

class.js
class User {
  constructor(name, age) {
    this.name = name;
    this.age  = age;
  }
}

// create objects from class
let sara = new User("Sara", 25);
let ali  = new User("Ali",  22);
console.log(sara.name); // "Sara"
output →Sara
Python

In Python, use class ClassName to define the blueprint. The __init__ method runs on creation. self refers to the object being created — same idea as this in JavaScript.

class.py
class User:
    def __init__(self, name, age):
        self.name = name
        self.age  = age

# create objects from class
sara = User("Sara", 25)
ali  = User("Ali",  22)
print(sara.name)  # "Sara"
output →Sara
faq 03

FAQs about
objects in coding

Quick, clear answers to the three questions beginners ask most about objects.

Q1 What is a class vs object in Python? +
class vs object

A class is a blueprint — it defines what an object looks like. An object is the actual thing built from that blueprint. You write the class once, then create as many objects from it as needed.

class = blueprint
Written once. Defines structure. No real data yet.
→
object = real thing
Built from class. Has actual data. Can make many.
Q2 Can objects have objects inside them? +
nested objects

Yes — and this is called a nested object. One object can contain another object as a value. This is how real apps store complex data like user profiles with addresses.

user = { “name”: “Sara”, “address”: { # object inside “city”: “Lahore” } } print(user[“address”][“city”]) # Lahore
Q3 What is the difference between object and array? +
object vs array

Arrays use numbered indexes — best for lists of similar items. Objects use named keys — best for describing one thing with many details. Quick rule: list of students → array. One student’s profile → object.

[ ] array
[“Ali”,”Sara”]
names[0] → “Ali”
{ } object
{“name”:”Sara”}
user[“name”] → “Sara”