TypeScript Math pow()

In this chapter, we will explore the Math.pow() method in TypeScript. This method returns the base raised to the power of the exponent. Understanding how to use Math.pow() is useful for performing exponentiation operations.

Table of Contents

  1. Definition
  2. Syntax
  3. Examples
  4. Conclusion

1. Definition

The Math.pow() method returns the value of a base raised to a specified power. This is commonly used for exponentiation operations in mathematical calculations.

2. Syntax

Math.pow(base, exponent);

Parameters

  • base: The base number.
  • exponent: The exponent to which the base number is raised.

Return Value

The method returns the value of the base raised to the exponent power.

3. Examples

Let's look at some examples to understand how Math.pow() works in TypeScript.

Example 1: Basic Usage

In this example, we use Math.pow() to calculate the power of a number.

let base: number = 2;
let exponent: number = 3;
let result = Math.pow(base, exponent);
console.log(result); // Output: 8

Example 2: Squaring a Number

In this example, we use Math.pow() to square a number (raising it to the power of 2).

let num: number = 5;
let result = Math.pow(num, 2);
console.log(result); // Output: 25

Example 3: Using Negative Exponents

In this example, we use Math.pow() with a negative exponent to calculate the reciprocal of a number raised to a positive power.

let base: number = 2;
let exponent: number = -3;
let result = Math.pow(base, exponent);
console.log(result); // Output: 0.125

Example 4: Using Zero as the Base

In this example, we use Math.pow() with zero as the base and a positive exponent.

let base: number = 0;
let exponent: number = 5;
let result = Math.pow(base, exponent);
console.log(result); // Output: 0

Example 5: Using Zero as the Exponent

In this example, we use Math.pow() with a non-zero base and zero as the exponent.

let base: number = 10;
let exponent: number = 0;
let result = Math.pow(base, exponent);
console.log(result); // Output: 1

Example 6: Using One as the Exponent

In this example, we use Math.pow() with any base and one as the exponent. The result should be the base itself.

let base: number = 7;
let exponent: number = 1;
let result = Math.pow(base, exponent);
console.log(result); // Output: 7

Example 7: Using Math.pow() with Expressions

In this example, we use Math.pow() with expressions as base and exponent.

let base: number = 2 + 3;
let exponent: number = 2 * 2;
let result = Math.pow(base, exponent);
console.log(result); // Output: 625

4. Conclusion

In this chapter, we explored the Math.pow() method in TypeScript, which is used to return the value of a base number raised to a specified exponent. We covered its definition, syntax, parameters, return value, and provided several examples to demonstrate its usage. Understanding how to use Math.pow() effectively can help in various mathematical calculations and scenarios where exponentiation operations are required.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare