DEV Community

JEGADESHWARAN B
JEGADESHWARAN B

Posted on

objects in JavaScript

🧠 Objects in JavaScript β€” A Simple and Complete Guide

After understanding functions as workers, the next important concept is objects.

If functions are workers, then:

Objects are like real-world things that store data and behavior together.


🌱 What is an Object? (First Principle)

An object is like a person or thing that has:

  • properties (data)
  • actions (functions)

πŸ§’ Real-Life Analogy

Think of a tea shop owner:

  • Name β†’ Arun
  • Age β†’ 25
  • Job β†’ Making tea

And he can:

  • make tea
  • take orders

πŸ‘‰ That is exactly what an object is.


βš™οΈ Basic Definition

An object is a collection of key-value pairs, where values can be data or functions.


β˜• Simple Example

```javascript id="k0f8kp"
const teaShop = {
owner: "Arun",
location: "Erode",
makeTea: function() {
console.log("Tea is ready");
}
};




---

πŸ‘‰ Here:

* `owner`, `location` β†’ properties
* `makeTea` β†’ function (method)

---

# πŸ“¦ Accessing Object Data



```javascript id="3zv2n5"
console.log(teaShop.owner);       // Arun
teaShop.makeTea();                // Tea is ready
Enter fullscreen mode Exit fullscreen mode

🧠 Objects + Functions (Important Connection)

When a function is inside an object, it is called a method.


```javascript id="k2p4zj"
const user = {
name: "Jegan",
greet() {
console.log("Hello " + this.name);
}
};




---

πŸ‘‰ `this.name` refers to the object itself.

---

# 🎭 The `this` Keyword

> `this` means β€œthe current object”



```javascript id="9f4gq2"
const person = {
    name: "Arun",
    speak() {
        console.log(this.name);
    }
};
Enter fullscreen mode Exit fullscreen mode

βœ” this = person object


🧾 Creating Objects (Different Ways)


1. Object Literal (Most Common)

```javascript id="3xvmbu"
const car = {
brand: "Toyota",
start() {
console.log("Car started");
}
};




---

## 2. Constructor Function



```javascript id="0m9z1p"
function Car(brand) {
    this.brand = brand;
}

const c1 = new Car("Honda");
Enter fullscreen mode Exit fullscreen mode

3. Using class (Modern Way)

```javascript id="p5yd4o"
class Car {
constructor(brand) {
this.brand = brand;
}
}




---

# πŸ” Objects are Dynamic

You can add or change properties anytime.



```javascript id="0z9xxf"
const user = {};

user.name = "Jegan";
user.age = 22;
Enter fullscreen mode Exit fullscreen mode

🧠 Objects are Reference Types

```javascript id="mbg1tx"
const a = { name: "A" };
const b = a;

b.name = "B";

console.log(a.name); // B




πŸ‘‰ Both point to the same object in memory.

---

# πŸ§ͺ Useful Object Methods

---

## Object.keys()



```javascript id="y4whbq"
Object.keys(teaShop);
Enter fullscreen mode Exit fullscreen mode

Object.values()

```javascript id="y5o0o1"
Object.values(teaShop);




---

## Object.entries()



```javascript id="8tvjv2"
Object.entries(teaShop);
Enter fullscreen mode Exit fullscreen mode

🌍 Real-World Example


User Object (CRM App)

```javascript id="mtu3kk"
const user = {
id: 1,
name: "Jegan",
email: "jegan@gmail.com",
login() {
console.log("User logged in");
}
};




---

## Transaction Object



```javascript id="4k2b7x"
const transaction = {
    amount: 5000,
    type: "expense",
    category: "rent"
};
Enter fullscreen mode Exit fullscreen mode

πŸ”— Objects + Functions Together

Objects store data
Functions operate on data

Together, they build applications.


🧠 Final Understanding

An object is a real-world entity in your program that groups related data and behavior together.


πŸ§’ Simple Definition (Child Level)

An object is like a person or thing that has information and can do actions.


πŸš€ Conclusion

Objects help you:

  • Organize data
  • Represent real-world things
  • Build structured applications

πŸ‘‰ In short:

Objects are containers of data and behavior in JavaScript.


If functions are workers,
then objects are people or things those workers belong to.


Top comments (0)