Properties and methods of JavaScript native objects

  • 2020-05-12 02:16:01
  • OfStack

Syntax for creating Number objects:


var myNum = new Number(value);
var myNum = Number(value);

When Number() and the operator new 1 are used as constructors, it returns a newly created Number object. If you call Number() as a function instead of the new operator, it converts its argument to a primitive value and returns this value (NaN if the conversion fails).

MAX_VALUE

The MAX_VALUE property is the largest number that can be represented in JavaScript. Its approximate value is 1.7976931348623157 x 10308. The largest negative number is -MAX_VALUE.

The number larger than MAX_VALUE is Infinity. MAX_VALUE is a static property, so the call method should be Number.MAX_VALUE.


console.log(Number.MAX_VALUE)  //1.7976931348623157e+308

MIN_VALUE

The MIN_VALUE property is the smallest number that can be represented in JavaScript (close to 0, but not negative). Its approximate value is 5 x 10-324.

All Numbers smaller than MIN_VALUE are converted to 0.

MIN_VALUE is a static property, so the call method should be Number.MIN_VALUE.

NaN

The NaN property is a special value that represents a non-numeric value. This property is used to indicate that a value is not a number. You can set the Number object to this value to indicate that it is not a numeric value.

You can use the isNaN() global function to determine if a value is NaN.

Number.NaN is a special value that indicates that the result of some arithmetic operation (such as taking the square root of a negative number) is not a number. The methods parseInt() and parseFloat() return this value if the specified string cannot be parsed. For functions that normally return significant Numbers, you can do the same, using Number.NaN to illustrate its error condition.

JavaScript outputs Number.NaN in the form NaN. Note that NaN is not always equal to other values, including itself. Therefore, you cannot test whether a value is a number by comparing it to Number.NaN, but you can only call isNaN() for comparison.

Note: the global variables NaN and Number.NaN are 1, while NaN is a non-configurable, non-modifiable property.


console.log(parseInt("abc"));    //NaN
console.log(NaN === NaN);        //false
console.log(Number.NaN === NaN); //false
console.log(isNaN(NaN));         //true
console.log(isNaN(Number.NaN));  //true

NEGATIVE_INFINITY

The NEGATIVE_INFINITY attribute represents a value less than -Number.MAX_VALUE. This value represents negative infinity.

When JavaScript displays NEGATIVE_INFINITY, -Infinity is used. The arithmetic behavior of this value is very similar to that of infinity. For example, any number multiplied by infinity is still infinity, and any number divided by infinity is 0.

-Infinity is the same as Number.NEGATIVE_INFINITY.


var x = (-Number.MAX_VALUE) * 2;
var y = Number.NEGATIVE_INFINITY;
console.log(x);     //-Infinity
console.log(y);     //-Infinity
console.log(x===y); //true

POSITIVE_INFINITY

The POSITIVE_INFINITY property represents a value greater than Number.MAX_VALUE. This value represents positive infinity.

JavaScript displays POSITIVE_INFINITY using Infinity. The arithmetic behavior of this value is very similar to that of infinity. For example, any number multiplied by infinity is still infinity, and any number divided by infinity is 0.

Infinity is equal to Number.POSITIVE_INFINITY.

The isFinite() method can determine whether the parameter is a finite number.


var x = Number.NEGATIVE_INFINITY;
var y = Number.POSITIVE_INFINITY;
var z = Infinity;
var a = "abc";
var b = 123; console.log(isFinite(x));  //false
console.log(isFinite(y));  //false
console.log(isFinite(z));  //false
console.log(isFinite(a));  //false
console.log(isFinite(b));  //true

toString()

The toString() method converts an Number object into a string and returns the result.

NumberObject.toString(radix)

The parameter radix is optional. Specifies the cardinality of a number so that it is an integer between 2 and 36. If this parameter is omitted, use cardinality 10, which is always recommended to avoid misunderstandings. For example, when radix is 2, NumberObject is converted to a string represented by a base 2 value.

An TypeError exception is thrown when the object calling the method is not Number.


var a = 100; console.log(a.toString());    //100
console.log(a.toString(10));  //100
console.log(a.toString(2));   //1100100
console.log(a.toString(8));   //144
console.log(a.toString(16));  //64

toLocaleString()

The toLocaleString() method converts an Number object into a string in the local format.

The string representation of a number, as determined by the implementation, is formatted according to the local specification and may affect the punctuation used for the decimal point or the thousandth separator.

An TypeError exception is thrown when the object calling the method is not Number.


var a = 123456; console.log(a.toLocaleString());  //123,456
console.log(a.toLocaleString("zh-Hans-CN-u-nu-hanidec"));  //123,456

More parameters can be found in MDN

toFixed()

The toFixed() method rounds Number 4 by 5 to a number with a specified number of decimal places.

NumberObject.toFixed(num)

The parameter num is required. Specify the number of decimal places between 0 and 20, including 0 and 20, and some implementations can support a larger range of values. If this parameter is omitted, it is replaced with a 0.

Returns the string representation of NumberObject, not using the exponential counting method, but a fixed num digit after the decimal point. If necessary, the number is rounded, or it can be topped with 0 so that it reaches the specified length. If num is greater than le+21, the method only calls NumberObject.toString (), returning a string represented by an exponential count.

An exception RangeError is thrown when num is too small or too large. Values between 0 and 20 do not raise this exception. Some implementations support a larger or smaller range of values. An TypeError exception is thrown when the object calling the method is not Number.


var n = 12345.6789; console.log(n.toFixed());            //12346
console.log(n.toFixed(2));           //12345.68
console.log(n.toFixed(6));           //12345.678900
console.log((1.23e+20).toFixed(2));  //123000000000000000000.00
console.log((1.23e-10).toFixed(2));  //0.00

Note: due to the treatment of floating point Numbers, the toFixed() method presents results that are not "4 rounds of 5" or "4 rounds of 6 pairs of 5", but rather a "4 rounds, 6 rounds, 5" representation of 10 confusion.


//Chrome In the
console.log(( 0.035 ).toFixed( 2 )); //0.04
console.log(( 0.045 ).toFixed( 2 )); //0.04

It is recommended that you write your own method to replace the default behavior of toFixed(). Please refer to the discussion on SO:


Number.prototype.toFixed = function(len){
    var temp = Math.pow(10,len);
    var s = Math.ceil(this * temp)
    return s/temp;
} console.log(( 0.035 ).toFixed( 2 ));  //0.04
console.log(( 0.045 ).toFixed( 2 ));  //0.05

toExponential()

The toExponential() method converts the value of an object to an exponential counting method.

NumberObject.toExponential(num)

The parameter num is optional. Specifies that the number of decimal places in an exponential count is between 0 and 20, including 0 and 20, and that some implementations can support a larger range of values. If this parameter is omitted, as many Numbers as possible are used.

Returns the string representation of NumberObject, using the exponential counting method, that is, one digit before the decimal point and num after the decimal point. The decimal part of the number is rounded off and topped up with 0 if necessary so that it reaches the specified length.

An exception is thrown when num is too small or too large. Values between 0 and 20 do not raise this exception. Some implementations support a larger or smaller range of values. An TypeError exception is thrown when the object calling the method is not Number.


console.log(Number.MAX_VALUE)  //1.7976931348623157e+308
0

toPrecision()

The toPrecision() method formats a value into a string in the form of a hexadecimal number.

NumberObject.toPrecision(num)

The parameter num is optional. Used to control the accuracy of Numbers. This parameter is a value between 1 and 21, including 1 and 21. If this parameter is omitted, the method toString() is called instead of converting the number to a decimal value.


var num = 10000.1234; console.log(num.toPrecision());    //10000.1234
console.log(num.toPrecision(2));   //1.0e+4
console.log(num.toPrecision(10));  //10000.12340


Related articles: