TypeScript Tutorial

TypeScript Math

Math.sqrt, Math.pow, Math.round, and Math.abs are built in. Use them instead of writing formulas by hand.

Math is already there

Arithmetic operators handle plus, minus, times, and divide. Square roots, powers, and rounding live on theMath object. You do not include a header. C++ needed <cmath>. TypeScript does not.

The functions take and return number. Store the result in a number unless you have a reason not to.

Compile the examples in /typescript/try with Try it in TypeScript. That is the TypeScript editor. Do not paste them into Python at /try or C++ at/cpp/try.

A complete program

This file uses the four functions you will see most often: Math.sqrt, Math.pow,Math.abs, and Math.round. Copy it, change the numbers, and compile again.

Example

const side: number = 9;
console.log("sqrt(9) = " + Math.sqrt(side));
console.log("pow(2, 8) = " + Math.pow(2, 8));
console.log("abs(-4.5) = " + Math.abs(-4.5));
console.log("round(2.6) = " + Math.round(2.6));

Math.sqrt

Math.sqrt(x) returns the square root of x. Pass a non-negative number. A negative argument yields NaN (not a number) instead of a useful result.

Distance formulas use square roots. The length of a right-triangle hypotenuse with legs 3 and 4 isMath.sqrt(3 * 3 + 4 * 4), which is 5.

Example

const a: number = 3;
const b: number = 4;
const c: number = Math.sqrt(a * a + b * b);
console.log(c);

Math.pow

Math.pow(base, exp) raises base to exp. Both arguments arenumber, so the result is a number even when the inputs look like integers.

For a small integer power you can still write n * n, or 2 ** 8. UseMath.pow when you want the call to be obvious, or when the exponent is already in a variable.

2 ** 8 is the same value as Math.pow(2, 8). This chapter usesMath.pow so the name matches the C++ pow lesson.

Math.abs and Math.round

Math.abs(x) returns the absolute value: negative numbers become positive, positive numbers stay positive. Use it when a difference might go either way and you only care about size.

Math.round(x) returns the nearest integer as a number. Halfway cases (such as 2.5) round toward plus infinity, so 2.5 becomes 3 and -2.5 becomes -2. That halfway rule differs from C++round, which rounds away from zero.

Example

console.log(Math.abs(-12));
console.log(Math.abs(7.25));
console.log(Math.round(3.2));
console.log(Math.round(3.8));
console.log(Math.round(2.5));
console.log(Math.round(-2.5));

The usual toolkit

CallMeaning
Math.sqrt(x)Square root of x
Math.pow(x, y)x to the power y
Math.abs(x)Absolute value of x
Math.round(x)Nearest integer
Math.floor(x)Largest integer not greater than x
Math.ceil(x)Smallest integer not less than x

Math.floor and Math.ceil live on the same object. You do not need them yet; know they exist so you do not reinvent them.

Types and mistakes

  • These functions return number. There is no separate integer type to truncate into.
  • Do not take Math.sqrt of a negative number if you expect a real result.
  • Math.pow is slower than a simple multiply. For squares, write x * x.

Next: boolean values, which math comparisons produce.

FAQ: TypeScript Math

Common questions about this page.

What is the StudyGrid TypeScript tutorial?

The StudyGrid TypeScript tutorial follows the same chapter rhythm as C++: syntax, types, input, loops, functions, classes, generics, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run typescript math examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn typescript math in this TypeScript TypeScript lesson (TypeScript Math).

Is the TypeScript editor the same as Try Python or Try C++?

No. Try TypeScript type-checks with tsc at /typescript/try and shows stdout plus compiler messages. Try Python stays at /try. Try C++ stays at /cpp/try. TypeScript lessons never open those editors.

Do I need to install a compiler to learn TypeScript?

No. Open a chapter, click Try it in TypeScript, and compile in the browser. You can also download a .ts file and compile locally with tsc.

Where should I start the TypeScript tutorial?

Start at TypeScript Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open TypeScript Examples, then generics, Map, and arrow functions. Use Next at the bottom of each chapter.

Is the TypeScript tutorial free?

Yes. The TypeScript workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.