惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

aimingoo的专栏
aimingoo的专栏
博客园 - 三生石上(FineUI控件)
GbyAI
GbyAI
大猫的无限游戏
大猫的无限游戏
M
MIT News - Artificial intelligence
Microsoft Azure Blog
Microsoft Azure Blog
月光博客
月光博客
Engineering at Meta
Engineering at Meta
I
InfoQ
T
Tailwind CSS Blog
N
Netflix TechBlog - Medium
S
SegmentFault 最新的问题
H
Help Net Security
博客园 - 【当耐特】
WordPress大学
WordPress大学
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
美团技术团队
博客园 - 叶小钗
T
The Blog of Author Tim Ferriss
腾讯CDC
雷峰网
雷峰网
Martin Fowler
Martin Fowler
The GitHub Blog
The GitHub Blog
D
Docker

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Objects in JavaScript
Annapoorani Kadhiravan · 2026-06-20 · via DEV Community
Cover image for Objects in JavaScript

Annapoorani Kadhiravan

Objects are one of the most important concepts in JavaScript. Almost everything in JavaScript revolves around objects.

Objects help us store related data together and represent real-world entities such as:

  • Students
  • Employees
  • Cars
  • Mobiles
  • Products
  • Users
  • Animals

What is an Object?

An object is a collection of properties and methods.

  • Properties store data.
  • Methods store functions.

Syntax

const objectName = {
    key1: value1,
    key2: value2
};


Example

const student = {
    name: "John",
    age: 21,
    city: "Chennai"
};

console.log(student);

Output:

{
    name: "John",
    age: 21,
    city: "Chennai"
}


Real-Life Example

Think of a Student ID Card.

Name : John
Age  : 21
City : Chennai

All these details belong to one student.

Similarly:

const student = {
    name: "John",
    age: 21,
    city: "Chennai"
};

The object stores related information together.


Why Do We Need Objects?

Without Objects

let name = "John";
let age = 21;
let city = "Chennai";

Managing multiple variables becomes difficult.

With Objects

const student = {
    name: "John",
    age: 21,
    city: "Chennai"
};

Everything is organized inside one object.


Object Terminologies

Consider:

const laptop = {
    brand: "HP",
    ram: "16GB",
    price: 70000
};

Here,

Term Value
brand Property
ram Property
price Property
"HP" Value
"16GB" Value
70000 Value

Creating Objects

There are several ways to create objects.


1. Object Literal

Most commonly used.

const mobile = {
    brand: "Samsung",
    model: "S25",
    price: 90000
};

console.log(mobile);

Output

{
brand: "Samsung",
model: "S25",
price: 90000
}


Real-Life Example

Writing details in a notebook.

Brand : Samsung
Model : S25
Price : 90000


Accessing Object Properties

There are two ways:

Dot Notation

const mobile = {
    brand: "Apple",
    model: "iPhone 16"
};

console.log(mobile.brand);

Output

Apple


Bracket Notation

console.log(mobile["model"]);

Output

iPhone 16


Real-Life Example

Finding a contact in your phone.

Contact Name → Mobile Number

Similarly,

mobile.brand

retrieves the value.


Adding Properties

Objects are dynamic.

const laptop = {
    brand: "Dell"
};

laptop.ram = "16GB";

console.log(laptop);

Output

{
brand: "Dell",
ram: "16GB"
}


Real-Life Example

Buying a laptop and later upgrading RAM.

Initially:

Brand : Dell

Later:

Brand : Dell
RAM : 16GB


Updating Properties

const laptop = {
    brand: "Dell",
    ram: "8GB"
};

laptop.ram = "16GB";

console.log(laptop);

Output

{
brand: "Dell",
ram: "16GB"
}


Real-Life Example

Changing your phone number in a bank account.

Old

9876543210

New

9999999999


Deleting Properties

const laptop = {
    brand: "HP",
    camera: "1080p"
};

delete laptop.camera;

console.log(laptop);

Output

{
brand: "HP"
}


Real-Life Example

Removing old information from a form.


CRUD Operations with Objects

CRUD means:

  • Create
  • Read
  • Update
  • Delete
const laptop = {
    brand: "Lenovo",
    ram: "8GB"
};

// Create
laptop.processor = "i7";

// Read
console.log(laptop.ram);

// Update
laptop.ram = "16GB";

// Delete
delete laptop.processor;

console.log(laptop);

Output

{
brand: "Lenovo",
ram: "16GB"
}


Objects with Methods

Methods are functions inside objects.

const student = {

    name: "John",

    greet: function() {
        console.log("Hello");
    }

};

student.greet();

Output

Hello


Real-Life Example

A Car

Properties

Brand
Color
Price

Actions

Start()
Stop()
Accelerate()

Actions are methods.


Using this Keyword

const student = {

    name: "John",

    greet: function() {
        console.log("Hello " + this.name);
    }

};

student.greet();

Output

Hello John


Real-Life Example

Suppose a teacher says:

"My class"

Here "my" refers to the teacher.

Similarly,

this.name

refers to the current object.


Nested Objects

Objects can contain other objects.

const employee = {

    name: "David",

    address: {
        city: "Chennai",
        state: "Tamil Nadu"
    }

};

console.log(employee.address.city);

Output

Chennai


Real-Life Example

Company

Company
    ↓
Department
        ↓
Employee

Similarly

Object
   ↓
Nested Object


Object.keys()

Returns all keys.

const student = {
    name: "John",
    age: 22
};

console.log(Object.keys(student));

Output

["name","age"]


Object.values()

Returns values.

console.log(Object.values(student));

Output

["John",22]


Object.entries()

Returns key-value pairs.

console.log(Object.entries(student));

Output

[
["name","John"],
["age",22]
]


Looping Through Objects

Using for...in

const student = {

    name: "John",
    age: 21,
    city: "Chennai"

};

for(let key in student){
    console.log(key, student[key]);
}

Output

name John
age 21
city Chennai


Real-Life Example

Checking items in a grocery bill one by one.


Object Destructuring

Extract values easily.

const laptop = {
    brand: "HP",
    ram: "16GB"
};

const {brand, ram} = laptop;

console.log(brand);
console.log(ram);

Output

HP
16GB


Real-Life Example

Removing specific books from a shelf instead of carrying the entire shelf.


Spread Operator with Objects

const student = {
    name: "John",
    age: 20
};

const details = {
    ...student,
    city: "Madurai"
};

console.log(details);

Output

{
name:"John",
age:20,
city:"Madurai"
}


Object.freeze()

Prevents modification.

const car = {
    brand: "BMW"
};

Object.freeze(car);

car.brand = "Audi";

console.log(car);

Output

{
brand:"BMW"
}


Real-Life Example

Submitting an exam paper.

After submission, changes cannot be made.


Object.seal()

Allows updating but prevents adding or deleting properties.

const car = {
    brand: "BMW"
};

Object.seal(car);

car.brand = "Audi";

console.log(car);

Output

{
brand:"Audi"
}


Object Constructor

const person = new Object();

person.name = "John";
person.age = 22;

console.log(person);

Output

{
name:"John",
age:22
}


Factory Function

function createStudent(name, age){

    return {
        name,
        age
    };

}

let s1 = createStudent("John",21);

console.log(s1);

Output

{
name:"John",
age:21
}


Constructor Function

function Student(name, age){

    this.name = name;
    this.age = age;

}

const s1 = new Student("John",21);

console.log(s1);

Output

{
name:"John",
age:21
}


Objects vs Arrays

Objects Arrays
Store data in key-value pairs Store values by index
Uses property names Uses index numbers
Represents entities Represents collections
Accessed using keys Accessed using indexes

Example

Object

const student = {
    name:"John",
    age:21
};

Array

const marks = [90,85,95];


Real-World Examples of Objects

Student

const student = {
    name: "John",
    age: 20,
    department: "CSE"
};


Car

const car = {
    brand: "BMW",
    color: "Black",
    price: 6000000
};


Employee

const employee = {
    id: 101,
    name: "David",
    salary: 50000
};


Product

const product = {
    name: "Laptop",
    price: 70000,
    stock: 20
};


References :
https://www.geeksforgeeks.org/javascript/objects-in-javascript/
https://www.w3schools.com/js/js_objects.asp