“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.
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.
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.
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.
age: 25
city: “Lahore”
color: “red”
speed: 120
storage: “128GB”
color: “black”
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:
name = "Sara"
age = 25
city = "Lahore"
email = "s (at) mail.com"
# 4 vars, no connectionuser = { "name": "Sara", "age": 25, "city": "Lahore" } # one place ✓
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.
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.
name = "Sara" age = 25 city = "Lahore" email = "s (at) mail.com" # 50 users = 200 vars 😩
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 use cases of objects
Every app you use stores its data in objects. Here’s what’s running behind the scenes:
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.
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”
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()
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.
user.age → 25
user.login() → true
( ) after the name.How to create
an object
Creating an object takes one line. Curly braces, key-value pairs, done.
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.
let user = { name: "Sara", age: 25, city: "Lahore" }; // access a property console.log(user.name); // "Sara"
Object syntax in Python
Python uses dictionaries — the same curly brace idea, but keys must be in quotes. Access with bracket notation: user["name"].
user = { "name": "Sara", "age": 25, "city": "Lahore" } # access a property print(user["name"]) # "Sara"
Empty object vs pre-filled object
Start empty and add later, or fill it right away — both work perfectly in real projects.
user = {}
# add later
user["name"] = "Sara"
user["age"] = 25user = {
"name": "Sara",
"age": 25,
"city": "Lahore"
}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 — 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).
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.
When to use dot vs bracket notation
Both do the same job. Choose based on your situation:
| situation | use dot | use bracket |
|---|---|---|
| Simple key name | user.name default | user[“name”] |
| Key has spaces | ✗ won’t work | user[“first name”] use this |
| Key stored in variable | ✗ won’t work | user[myKey] use this |
| Python (dict) | ✗ not for dicts | user[“name”] always |
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.
Assign a value to a key that doesn’t exist yet. The object creates it on the spot — no setup needed.
user = {"name": "Sara"} # add new property user["email"] = "s (at) mail.com" print(user)
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": 25} # update existing value user["age"] = 26 print(user["age"])
Use del in Python or delete in JavaScript before the property — it removes the key and its value completely from the object.
user = { "name": "Sara", "city": "Lahore" } # remove city del user["city"]
Methods — functions
inside objects
Properties store data. Methods perform actions. Together, they make objects complete and powerful.
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.
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.
name: “Sara”,
greet: fn…
}
= “Sara”
same thing
Method example in JavaScript
let user = { name: "Sara", age: 25, greet: function() { return "Hi, I'm " + this.name; } // ↑ this = user }; console.log(user.greet());
Method example in Python
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())
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.
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.
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.
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:
user = { "name": "Sara", "age": 25, "address": { "city": "Lahore", "country": "Pakistan" } } print(user["address"]["city"])
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.
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(user) // returns: ["name", "age", "city"]
Object.values(user) // returns: ["Sara", 25, "Lahore"]
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.
const user = { name: "Sara", age: 25 }; for (let key in user) { console.log(key + ": " + user[key]); } // name: Sara // age: 25
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.
let name = user.name;
let age = user.age;
let city = user.city;
// 3 separate lineslet { name, age, city } = user; // one line ✓ // same result
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.
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.
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?
- You have a list of similar items
- Order matters
- You need to loop through all
- Items have different labels
- You describe one specific thing
- You access by name, not position
| feature | [ ] array | { } object |
|---|---|---|
| Access by | Index [0] | Key “name” |
| Best for | Lists of items | One thing, many details |
| Order | Always preserved | Not guaranteed |
| Example | [“Ali”,”Sara”] | {name:”Sara”,age:25} |
| Loop with | for…of / forEach | for…in / Object.keys |
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.
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.
ali = User(“Ali”)
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 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"
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 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"
FAQs about
objects in coding
Quick, clear answers to the three questions beginners ask most about objects.
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.
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.
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.
names[0] → “Ali”
user[“name”] → “Sara”