How can I call a function within a class?

In a class, functions are called methods, and you can call them in two main ways:

  1. Using an instance of the class (for instance methods).
  2. Directly from the class (for static methods).

Here’s how you can call a function (method) within a class in different scenarios:

1. Calling an Instance Method

Instance methods require an object (instance of the class) to call them because they typically operate on the instance's data.

Example:

class Person { constructor(name, age) { this.name = name; this.age = age; } // Instance method greet() { console.log(`Hi, my name is ${this.name} and I'm ${this.age} years old.`); } } // Create an instance of the class const person = new Person('Alice', 30); // Call the instance method using the instance person.greet(); // Output: Hi, my name is Alice and I'm 30 years old.

2. Calling a Static Method

Static methods belong to the class itself and do not require an instance to call them. They are defined using the static keyword and cannot directly access instance properties or methods.

Example:

class MathUtils { // Static method static add(a, b) { return a + b; } } // Call the static method directly on the class console.log(MathUtils.add(3, 5)); // Output: 8

3. Calling Another Method Within the Same Class

You can call another method within the same class using this (for instance methods) or the class name (for static methods).

Example:

class Calculator { // Instance method multiply(a, b) { return a * b; } // Instance method calling another instance method square(num) { return this.multiply(num, num); // Calling multiply() using `this` } } // Create an instance of the class const calc = new Calculator(); // Call the methods console.log(calc.multiply(3, 4)); // Output: 12 console.log(calc.square(5)); // Output: 25

For static methods:

class MathUtils { // Static method static square(num) { return MathUtils.multiply(num, num); // Calling another static method } // Another static method static multiply(a, b) { return a * b; } } // Call the static methods console.log(MathUtils.square(4)); // Output: 16

4. Private Methods (Optional in Modern JavaScript)

You can define private methods using the # prefix, which can only be accessed within the class.

Example:

class BankAccount { constructor(balance) { this.balance = balance; } // Private method #validateAmount(amount) { return amount > 0; } // Public method calling the private method deposit(amount) { if (this.#validateAmount(amount)) { this.balance += amount; console.log(`Deposited: $${amount}`); } else { console.log('Invalid amount'); } } } const account = new BankAccount(100); account.deposit(50); // Output: Deposited: $50 // account.#validateAmount(50); // Error: Private field '#validateAmount' must be declared in an enclosing class

Summary

  1. Call instance methods using an instance: object.methodName().
  2. Call static methods using the class name: ClassName.methodName().
  3. Within a class:
    • Use this.methodName() for instance methods.
    • Use ClassName.methodName() for static methods.

This approach ensures clear and efficient access to class methods. For mastering object-oriented principles in JavaScript, explore Grokking Advanced Coding Patterns for Interviews on DesignGurus.io!

TAGS
Coding Interview
CONTRIBUTOR
Arslan Ahmad
Arslan Ahmad
ex-FAANG engineering manager and author or Grokking series.

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
What is the Software Development Life Cycle?
What Is the xAI Interview Process Like? (Round by Round)
xAI's fast, engineer-led hiring loop explained: the Statement of Exceptional Work, practical coding rounds, system design, the final presentation, and a 2-to-3-week timeline.
Which is the best YouTube channel to learn DSA?
What is render() in ReactJS?
Why does DevOps fail?
What is the latest version of Android?
Related Courses
New
Grokking Agentic Design Patterns course cover
Grokking Agentic Design Patterns
Design agents that act safely, recover from failure, and justify their cost.
4.9
(561 learners)
Discounted price for Your Region

$98

New
Grokking the AI System Design Interview course cover
Grokking the AI System Design Interview
Learn to design AI systems the way interviewers expect: classic ML products, LLM and RAG architectures, and agentic systems, all through the lens of the system design interview.
4.6
(3,192 learners)
Discounted price for Your Region

$98

Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Design Gurus logo
One-Stop Portal For Tech Interviews.
About Us
Our Team
Careers
Partners
Contact Us
Become Affiliate
Copyright © 2026 Design Gurus, LLC. All rights reserved.