Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Math — mathematical constants and functions

Math is a built-in ECMAScript object providing mathematical constants and functions for Number values. Use its methods without creating an instance, and distinguish rounding down, rounding up, rounding to the nearest integer, and truncation.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

Math.PI
Math.abs(x)
Math.floor(x)
Math.ceil(x)
Math.round(x)
Math.trunc(x)
Math.min(...values)
Math.max(...values)
Math.sqrt(x)
Math.random()

Math is a built-in ECMAScript object, not a constructor or a Web API. Use Math.abs(x) or the constant Math.PI; Math() and new Math() throw TypeError. No import is needed.

Math.abs(x) returns the absolute value, and Math.sqrt(x) returns the square root. Math.min(...values) and Math.max(...values) select the smallest and largest values; the ... operator passes the array elements as separate arguments.

Math.floor(x) rounds toward −∞, Math.ceil(x) toward +∞, and Math.trunc(x) removes the fractional part toward zero. Math.round(x) selects the nearest integer, breaking ties toward +∞: Math.round(-3.5) produces -3, not -4. Some negative arguments can produce -0.

The numeric methods described here operate on Number, not BigInt; for example, Math.abs(1n) throws TypeError. Floating-point arithmetic has limited precision, and Math.sqrt(-1) returns NaN. Math.random() returns a value in [0, 1); it does not guarantee cryptographic security. The example avoids randomness to keep its output deterministic.

Examples

const values = [-3.5, 2, 9];
console.log(Math.abs(values[0]));
console.log(Math.floor(values[0]));
console.log(Math.ceil(values[0]));
console.log(Math.round(values[0]));
console.log(Math.trunc(values[0]));
console.log(Math.min(...values));
console.log(Math.max(...values));
console.log(Math.sqrt(values[2]));

Result

3.5
-4
-3
-3
-3
-3.5
9
3

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources