⚙️ Methods

Last Updated: 24th October 2025


Methods are functions defined inside a class that describe the behavior of objects.
They help objects do actions like display(), calculate(), etc.

Hinglish Tip 🗣: Method ek function hi hai jo class ke andar likha jata hai aur object ke data par kaam karta hai.


🧩 1. Instance Methods

  • The most common type of method.
  • Works with object (instance) data using the this keyword.
  • You must create an object to call it.

Example:

class Student {
  constructor(name, marks) {
    this.name = name;
    this.marks = marks;
  }

  // instance method
  showDetails() {
    console.log(`Name: ${this.name}, Marks: ${this.marks}`);
  }
}

const s1 = new Student("Riya", 88);
s1.showDetails(); // Name: Riya, Marks: 88

Hinglish Tip 🗣: Instance methods “object ke data” par kaam karte hain — isliye unhe call karne ke liye object banana padta hai.


🧩 2. Static Methods

  • Declared using the static keyword.
  • Belongs to the class itself, not to any object.
  • Called using the class name, not an object.
  • Used for utility or helper functions (e.g., calculations, formatting).
class MathHelper {
  static add(a, b) {
    return a + b;
  }
}

console.log(MathHelper.add(5, 7)); // 12 ✅

Hinglish Tip 🗣: Static methods “class ke liye” kaam karte hain, kisi object ke liye nahi — unhe direct class se call karte hain.


💡 Quick Practice