Number
JavaScript does not separate integer numbers and numbers with floating point. It has only the Number type, that corresponds to double type in other languages.
Be carefull while using + operator with numbers and strings. When both operands are numbers, result will be also number. If one operand string, other operand will be converted to string and result will be string. So result of 10+15+"25" will be string "2525", and not "101525" or 50. Other arithmetic operations work only with numbers, so strings also will be converted to numbers.
Do not confuse a primitive number value with objects of type Number when using operators=== or instanceof.
// "2525"
console.log(10+15+"25");
// numbers 75 and 100
console.log("100"-"25", "10"*"10");
// false, comparing primitive with object
console.log(10===new Number("10"));
// true, comparing two primitives
console.log(10===Number("10"));
// false
console.log(10 instanceof Number);
// true
console.log(new Number("10") instanceof Number);
methods
method | description |
---|---|
toExponential([fd]) | Returns a string representing the number in exponential notation. An optional parameter specifies the number of digits after the decimal point. |
toFixed([d]) | Returns a string representing the given number using fixed-point notation. An optional parameter specifies the number of digits to appear after the decimal point, possible value is in a range [1; 20]. |
toPrecision([p]) | Returns a string representing a Number object in fixed-point or exponential notation rounded to precision significant digits. An optional parameter specifies the number of significant digits. |
toString([radix]) | Returns a string representation of number. An optional parameter specifies a radix, possible values is in a range [2; 36]. |
valueOf() | Returns the primitive value of the Number object. |
static properties
property | description |
---|---|
EPSILON | The smallest interval between two representable numbers. |
MAX_SAFE_INTEGER | The maximum safe integer: 253 - 1. |
MAX_VALUE | The largest positive representable number: 21024. |
MIN_SAFE_INTEGER | The minimum safe integer: -(253 - 1). |
MIN_VALUE | The smallest positive representable number, that is the positive number closest to zero: 5e-324. |
NEGATIVE_INFINITY | Special value representing negative infinity. Returned on overflow. |
POSITIVE_INFINITY | Special value representing infinity. Returned on overflow. |
static methods
method | description |
---|---|
isNaN(x) | Tests whether the passed value is NaN (i.e Not a Number). |
isFinite(x) | Tests whether the passed value is a finite number. |
isInteger(x) | Tests whether the passed value is an integer. |
isSafeInteger(x) | Tests whether the passed value is a safe integer. |
parseFloat(str) | Parses an argument and returns a floating point number. If a number cannot be parsed from the argument, it returns NaN. This is the same as the global parseFloat() function. |
parseInt(str [, radix]) | Parses an argument and returns an integer of the specified radix. Possible values of optional parameter radix is in a range [2; 36], other values will result to NaN. This is the same as the global parseInt() function. |