Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Class Methods | Classes Fundamentals
TypeScript Classes and OOP

bookClass Methods

1234567891011121314151617181920212223242526
class Calculator { add(a: number, b: number): number { return a + b; } subtract(a: number, b: number): number { return a - b; } multiply(a: number, b: number): number { return a * b; } divide(a: number, b: number): number { if (b === 0) { throw new Error("Cannot divide by zero."); } return a / b; } } const calc = new Calculator(); console.log(calc.add(5, 3)); // 8 console.log(calc.subtract(10, 4)); // 6 console.log(calc.multiply(2, 7)); // 14 console.log(calc.divide(20, 5)); // 4
copy

When you define a function inside a TypeScript class, that function is called a method. Methods are used to perform actions or calculations related to the class's purpose. You can declare methods by writing the method name, followed by parentheses and an optional parameter list, a colon, and the return type. The method body is enclosed in curly braces.

To call a method, create an instance of the class and use dot notation, such as instance.methodName(). Methods can return any type, including number, string, boolean, or even custom types. If a method does not explicitly return a value, its return type is void. Specifying the return type helps TypeScript check that your method returns the correct kind of value.

In the code above, the Calculator class defines four methods: add, subtract, multiply, and divide. Each method takes two numbers as parameters and returns a number. The divide method also checks for division by zero and throws an error if needed. When you create a new Calculator instance, you can call these methods to perform calculations.

question mark

What is a method in the context of a TypeScript class?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 3

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Awesome!

Completion rate improved to 5

bookClass Methods

Swipe to show menu

1234567891011121314151617181920212223242526
class Calculator { add(a: number, b: number): number { return a + b; } subtract(a: number, b: number): number { return a - b; } multiply(a: number, b: number): number { return a * b; } divide(a: number, b: number): number { if (b === 0) { throw new Error("Cannot divide by zero."); } return a / b; } } const calc = new Calculator(); console.log(calc.add(5, 3)); // 8 console.log(calc.subtract(10, 4)); // 6 console.log(calc.multiply(2, 7)); // 14 console.log(calc.divide(20, 5)); // 4
copy

When you define a function inside a TypeScript class, that function is called a method. Methods are used to perform actions or calculations related to the class's purpose. You can declare methods by writing the method name, followed by parentheses and an optional parameter list, a colon, and the return type. The method body is enclosed in curly braces.

To call a method, create an instance of the class and use dot notation, such as instance.methodName(). Methods can return any type, including number, string, boolean, or even custom types. If a method does not explicitly return a value, its return type is void. Specifying the return type helps TypeScript check that your method returns the correct kind of value.

In the code above, the Calculator class defines four methods: add, subtract, multiply, and divide. Each method takes two numbers as parameters and returns a number. The divide method also checks for division by zero and throws an error if needed. When you create a new Calculator instance, you can call these methods to perform calculations.

question mark

What is a method in the context of a TypeScript class?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 3
some-alt