본문 바로가기
Web/JavaScript

[JavaScript] Number Object

by llHoYall 2022. 7. 7.

The Number object is a standard built-in object that is useful when handling the primitive type of numbers.

Number Definition

We can create an instance whose type is the Number using the Number() constructor.

const numObj = new Number(); // [Number: 0]
const numObj = new Number(10); // [Number: 10]
const numObj = new Number('10'); // [Number: 10]
const numObj = new Number('test'); // [Number: NaN]

However, this is not often used.

It returns the value of the primitive type not an instance of the Number if we use it without the new operator.

Number('-5'); // -5
Number('3.14'); // 3.14
Number('test'); // NaN
Number(true); // 1
Number(false); // 0

Number Property

EPSILON

This property is used to solve errors caused by floating points.

It was introduced in ES6.

function isEqual(x, y) {
  return Math.abs(x - y) < Number.EPSILON;
}

0.1 + 0.2 === 0.3; // false
isEqual(0.1 + 0.2, 0.3); // true

MAX_VALUE & MIN_VALUE

These properties are the maximum and minimum positive numeric values representable in JS.

It was introduced in ES6.

Number.MAX_VALUE; // 1.7976931348623157e+308
Number.MIN_VALUE; // 5e-324

MAX_SAFE_INTEGER & MIN_SAFE_INTEGER

These properties are the maximum and minimum safe integers in JS.

Number.MAX_SAFE_INTEGER; // 9007199254740991 (2^53 - 1)
Number.MIN_SAFE_INTEGER; // -9007199254740991 (-(2^53 - 1))

POSITIVE_INFINITY & NEGATIVE_INFINITY

These properties are the positive and negative Infinity values.

Number.POSITIVE_INFINITY; // Infinity
Number.NEGATIVE_INFINITY; // -Infinity

NaN

This property represents Not-a-Number.

Number.NaN; // NaN

Number Method

The built-in global functions have implicit type coercion to number types, but the methods of the Number object do not have implicit type coercion.

isFinite()

This method determines whether the passed value is a finite number.

It was introduced in ES6.

Number.isFinite(0); // true
Number.isFinite(Number.MAX_VALUE); // true
Number.isFinite(Infinity); // false
Number.isFinite(NaN); // false

isFinite(null); // true
Number.isFinite(null); // false
isFinite(true); // true
Number.isFinite(true); // false

isInteger()

This method determines whether the passed value is an integer.

It was introduced in ES6.

Number.isInteger(0); // true
Number.isInteger(-5); // true
Number.isInteger(3.14); // false
Number.isInteger('2'); // false
Number.isInteger(true); // false
Number.isInteger(Infinity); // false

isNaN()

This method determines whether the passed value is NaN and its type is Number.

It was introduced in ES6.

Number.isNaN(NaN); // true

isNaN(undefined); // true
Number.isNaN(undefined); // false

isSafeInteger()

This method determines whether the provided value is a number that is a safe integer.

The safe integer value is between -(2^53 - 1) ~ (2^53 - 1).

It was introduced in ES6.

Number.isSafeInteger(0); // true
Number.isSafeInteger(1234567890); // true
Number.isSafeInteger(-1234567890); // true
Number.isSafeInteger(3.14); // false
Number.isSafeInteger(10000000000000000); // false
Number.isSafeInteger('2'); // false

parseFloat()

This method parses an argument and returns a floating-point number.

Number.parseFloat(2); // 2
Number.parseFloat(3.14); // 3.14
Number.parseFloat('2'); // 2
Number.parseFloat('3.14'); // 3.14

parseInt()

This method parses a string argument and returns an integer of the specified radix or base.

Number.parseInt(2); // 2
Number.parseInt(3.14); // 3
Number.parseInt('2'); // 2
Number.parseInt('3.14'); // 3
Number.parseInt('1111011', 2); // 123
Number.parseInt('173', 8); // 123
Number.parseInt('7b', 16); // 123

toExponential()

This method returns a string representing the Number object in exponential notation.

It accepts the number of digits below the decimal point as a parameter.

(123.4567).toExponential(); // 1.234567e+2
(123.4567).toExponential(1); // 1.2e+2
(123.4567).toExponential(3); // 1.235e+2

toFixed()

This method formats a number using fixed-point notation.

It accepts the number of digits below the decimal point as a parameter.

(12.56).toFixed(); // 13
(12.56).toFixed(1); // 12.6
(12.56).toFixed(2); // 12.56
(12.56).toFixed(3); // 12.560

toLocaleString()

This method returns a string with a language-sensitive representation of this number.

It can accept the locales as a first parameter and options as a second parameter.

We can enter multiple locales in an array and try to apply them in order. If the current one is not supported, try to apply the following.

(12345).toLocaleString(); // U.S. (12,345)
(12345).toLocaleString('en-IN'); // India (12,345)
(12345).toLocaleString('zh-Hans-CN-u-nu-hanidec'); // Chinese (一二,三四五)
(12345).toLocaleString(['ban', 'id']); // Balinese, Indonesian (12.345)
(12345).toLocaleString('de-DE', { style: 'currency', currency: 'EUR' }); // 12.345,00 €
(12345).toLocaleString('ja-JP', { style: 'currency', currency: 'JPY' }) // ¥12,345

toPrecision()

This method returns a string representing the Number object to the specified precision by rounding.

It accepts the total number of digits as a parameter.

(123.456).toPrecision(); // 123.456
(123.456).toPrecision(1); // 1e+2
(123.456).toPrecision(2); // 1.2e+2

toString()

This method returns a string representing the specified Number object.

It can accept the radix or base as a parameter.

(123).toString(); // 123
(123).toString(2); // 1111011
(123).toString(8); // 173
(123).toString(16); // 7b

'Web > JavaScript' 카테고리의 다른 글

[JavaScript] Date Object  (0) 2022.07.09
[JavaScript] Math Object  (0) 2022.07.08
[JavaScript] Array  (0) 2022.07.06
[JavaScript] Class  (0) 2022.07.01
[JavaScript] Object  (0) 2022.06.26

댓글