Numbers API Flashcards

1
Q

Number properties

A
  • Number.MAX_VALUE - The largest positive representable number (1.7976931348623157e+308)
  • Number.MIN_VALUE - The smallest positive representable number (5e-324)
  • Number.NaN - Special “not a number” value
  • Number.NEGATIVE_INFINITY - Special negative infinite value; returned on overflow
  • Number.POSITIVE_INFINITY - Special positive infinite value; returned on overflow
  • Number.EPSILON - Difference between 1 and the smallest value greater than 1 that can be represented as a Number (2.220446049250313e-16)
  • Number.MIN_SAFE_INTEGER - Minimum safe integer in JavaScript (−2^53 + 1, or −9007199254740991)
  • Number.MAX_SAFE_INTEGER - Maximum safe integer in JavaScript (+2^53 − 1, or +9007199254740991)

“Number object” (MDN Web Docs). Retrieved February 26, 2024.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Number.isFinite()

A

The Number.isFinite() static method determines whether the passed value is a finite number — that is, it checks that a given value is a number, and the number is neither positive Infinity, negative Infinity, nor NaN.

Syntax

Number.isFinite(value)

Parameters

  • value - The value to be tested for finiteness.

Return value

  • The boolean value true if the given value is a finite number. Otherwise false.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Number.isInteger()

A

The Number.isInteger() static method determines whether the passed value is an integer.

Syntax

Number.isInteger(value)

Parameters

  • value - The value to be tested for being an integer.

Return value

  • The boolean value true if the given value is an integer. Otherwise false.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Number.isNaN()

A

The Number.isNaN() static method determines whether the passed value is the number value NaN, and returns false if the input is not of the Number type. It is a more robust version of the original, global isNaN() function.

Syntax

Number.isNaN(value)

Parameters

  • value - The value to be tested for NaN.

Return value

The boolean value true if the given value is a number with value NaN. Otherwise, false.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Number.isSafeInteger()

A

The Number.isSafeInteger() static method determines whether the provided value is a number that is a safe integer.

A safe integer is an integer that can be exactly represented as an IEEE-754 double precision number

Syntax

Number.isSafeInteger(testValue)

Parameters

  • testValue - The value to be tested for being a safe integer.

Return value

  • The boolean value true if the given value is a number that is a safe integer. Otherwise false.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Number.parseFloat()

A

The Number.parseFloat() static method parses an argument and returns a floating point number. If a number cannot be parsed from the argument, it returns NaN.

Syntax

Number.parseFloat(string)

Parameters

  • string - The value to parse, coerced to a string. Leading whitespace in this argument is ignored.

Return value

  • A floating point number parsed from the given string.
  • Or NaN when the first non-whitespace character cannot be converted to a number.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Number.parseInt()

A

The Number.parseInt() static method parses a string argument and returns an integer of the specified radix or base.

Syntax

Number.parseInt(string)
Number.parseInt(string, radix)

Parameters

  • string - The value to parse, coerced to a string. Leading whitespace in this argument is ignored.
  • radix Optional - An integer between 2 and 36 that represents the radix (the base in mathematical numeral systems) of the string.

If radix is undefined or 0, it is assumed to be 10 except when the number begins with the code unit pairs 0x or 0X, in which case a radix of 16 is assumed.

Return value
An integer parsed from the given string.

If the radix is smaller than 2 or bigger than 36, or the first non-whitespace character cannot be converted to a number, NaN is returned.

Note - This method has the same functionality as the global parseInt() function:

Number.parseInt === parseInt; // true
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Number.prototype.toExponential()

A

The toExponential() method of Number values returns a string representing this number in exponential notation.

Syntax

toExponential()
toExponential(fractionDigits)

Parameters

  • fractionDigits Optional - An integer specifying the number of digits after the decimal point. Defaults to as many digits as necessary to specify the number.

Return value

A string representing the given Number object in exponential notation with one digit before the decimal point, rounded to fractionDigits digits after the decimal point.

Exceptions

  • RangeError - Thrown if fractionDigits is not between 0 and 100 (inclusive).
  • TypeError Thrown if this method is invoked on an object that is not a Number.

Examples:

const numObj = 77.1234;

console.log(numObj.toExponential()); // 7.71234e+1
console.log(numObj.toExponential(4)); // 7.7123e+1
console.log(numObj.toExponential(2)); // 7.71e+1
console.log((77.1234).toExponential()); // 7.71234e+1
console.log((77).toExponential()); // 7.7e+1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Number.prototype.toFixed()

A

The toFixed() method of Number values formats this number using fixed-point notation.

Syntax

toFixed()
toFixed(digits)

Parameters

  • digits Optional - The number of digits to appear after the decimal point; should be a value between 0 and 100, inclusive. If this argument is omitted, it is treated as 0.

Return value

A string representing the given number using fixed-point notation.

Exceptions

  • RangeError - Thrown if digits is not between 0 and 100 (inclusive).
  • TypeError - Thrown if this method is invoked on an object that is not a Number.

Using toFixed() with negative numbers

Because member access has higher precedence than unary minus, you need to group the negative number expression to get a string.

-2.34.toFixed(1); // -2.3, a number
(-2.34).toFixed(1); // '-2.3'

Examples:

const numObj = 12345.6789;

numObj.toFixed(); // '12346'; rounding, no fractional part
numObj.toFixed(1); // '12345.7'; it rounds up
numObj.toFixed(6); // '12345.678900'; additional zeros
(1.23e20).toFixed(2); // '123000000000000000000.00'
(1.23e-10).toFixed(2); // '0.00'
(2.34).toFixed(1); // '2.3'
(2.35).toFixed(1); // '2.4'; it rounds up
(2.55).toFixed(1); // '2.5'
// it rounds down as it can't be represented exactly by a float and the
// closest representable float is lower
(2.449999999999999999).toFixed(1); // '2.5'
// it rounds up as it's less than Number.EPSILON away from 2.45.
// This literal actually encodes the same number value as 2.45

(6.02 * 10 ** 23).toFixed(50); // 6.019999999999999e+23; large numbers still use exponential notation
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Number.prototype.toLocaleString()

A

The toLocaleString() method of Number values returns a string with a language-sensitive representation of this number. In implementations with Intl.NumberFormat API support, this method simply calls Intl.NumberFormat.

Every time toLocaleString is called, it has to perform a search in a big database of localization strings, which is potentially inefficient. When the method is called many times with the same arguments, it is better to create a Intl.NumberFormat object and use its format() method, because a NumberFormat object remembers the arguments passed to it and may decide to cache a slice of the database, so future format calls can search for localization strings within a more constrained context.

Syntax

toLocaleString()
toLocaleString(locales)
toLocaleString(locales, options)

Parameters

  • locales Optional - A string with a BCP 47 language tag, or an array of such strings. Corresponds to the locales parameter of the Intl.NumberFormat() constructor.

In implementations without Intl.NumberFormat support, this parameter is ignored and the host’s locale is usually used.

  • options Optional - An object adjusting the output format. Corresponds to the options parameter of the Intl.NumberFormat() constructor.

In implementations without Intl.NumberFormat support, this parameter is ignored.

Return value
A string representing the given number according to language-specific conventions.

In implementations with Intl.NumberFormat, this is equivalent to new Intl.NumberFormat(locales, options).format(number).

Examples:

const number = 123456.789;

// German uses comma as decimal separator and period for thousands
console.log(number.toLocaleString("de-DE"));
// 123.456,789

// Arabic in most Arabic speaking countries uses Eastern Arabic digits
console.log(number.toLocaleString("ar-EG"));
// ١٢٣٤٥٦٫٧٨٩

// India uses thousands/lakh/crore separators
console.log(number.toLocaleString("en-IN"));
// 1,23,456.789

// the nu extension key requests a numbering system, e.g. Chinese decimal
console.log(number.toLocaleString("zh-Hans-CN-u-nu-hanidec"));
// 一二三,四五六.七八九

// when requesting a language that may not be supported, such as
// Balinese, include a fallback language, in this case Indonesian
console.log(number.toLocaleString(["ban", "id"]));
// 123.456,789
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Number.prototype.toPrecision()

A

The toPrecision() method of Number values returns a string representing this number to the specified precision.

Syntax

toPrecision()
toPrecision(precision)

Parameters

  • precision Optional - An integer specifying the number of significant digits.

Return value

A string representing a Number object in fixed-point or exponential notation rounded to precision significant digits.

If the precision argument is omitted, behaves as Number.prototype.toString(). If the precision argument is a non-integer value, it is rounded to the nearest integer.

Exceptions

  • RangeError - Thrown if precision is not between 1 and 100 (inclusive).

Examples:

let num = 5.123456;

console.log(num.toPrecision()); // '5.123456'
console.log(num.toPrecision(5)); // '5.1235'
console.log(num.toPrecision(2)); // '5.1'
console.log(num.toPrecision(1)); // '5'

num = 0.000123;

console.log(num.toPrecision()); // '0.000123'
console.log(num.toPrecision(5)); // '0.00012300'
console.log(num.toPrecision(2)); // '0.00012'
console.log(num.toPrecision(1)); // '0.0001'

// note that exponential notation might be returned in some circumstances
console.log((1234.5).toPrecision(2)); // '1.2e+3'
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Number.prototype.toString()

A

The toString() method of Number values returns a string representing this number value.

Syntax

toString()
toString(radix)

Parameters

  • radix Optional - An integer in the range 2 through 36 specifying the base to use for representing the number value. Defaults to 10.

Return value

A string representing the specified number value.

Exceptions

  • RangeError - Thrown if radix is less than 2 or greater than 36.

Examples:

const count = 10;
console.log(count.toString()); // "10"

console.log((17).toString()); // "17"
console.log((17.2).toString()); // "17.2"

const x = 6;
console.log(x.toString(2)); // "110"
console.log((254).toString(16)); // "fe"
console.log((-10).toString(2)); // "-1010"
console.log((-0xff).toString(2)); // "-11111111"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Number.prototype.valueOf()

A

The valueOf() method of Number values returns the value of this number.

This method is usually called internally by JavaScript and not explicitly in web code.

Syntax

valueOf()

Parameters

None.

Return value

A number representing the primitive value of the specified Number object.

Examples:

const numObj = new Number(10);
console.log(typeof numObj); // object

const num = numObj.valueOf();
console.log(num); // 10
console.log(typeof num); // number
How well did you know this?
1
Not at all
2
3
4
5
Perfectly