In this article, you are going to learn all about JavaScript Number Methods with the help of examples. In the previous tutorial, we have seen all about JavaScript Numbers using examples.
JavaScript number methods are used to work with number in JavaScript.
Before going through this article, you should have basic knowledge of JavaScript Numbers.
Headings of Contents
JavaScript Number Methods and Attributes
JavaScript Number provides various methods and attributes that is very useful, when you want to work with number in your JavaScript project or code.
JavaScript Number Methods
The toString() method
The toString() method returns number as string.
Example:
let x = 120;
console.log(x.toString()); // return 123 as string.
console.log((210).toString()); // return 210 as string.
console.log((120 + 80).toString()); // return 200 as string.
Note:- All JavaScript number methods can be used with any type of number ( variable, expression, and literals )
The toExponential() Method
The toExponential() method is used to return, with a number rounded and written using exponential expression.
Example:
let x = 10.3489478;
console.log(x.toExponential(3)); // 1.035e+1
console.log(x.toExponential(4)); // 1.0349e+1
console.log(x.toExponential(6)); // 1.034895e+1
The toFixed() method
The toFixed() method is used to return a number as a string, with the number with a specified number of decimals.
Example:
let x = 10.3489478;
console.log(x.toFixed(3)); // 1.035
console.log(x.toFixed(4)); // 1.0349
console.log(x.toFixed(6)); // 1.034895
The toPrecision() method
The toPrecision() method returns string. This method takes one argument as a number that refers to the length of the output.
Example:
let x = 10.3489478;
console.log(x.toPrecision(3)); // 1.03
console.log(x.toPrecision(4)); // 1.034
console.log(x.toPrecision(6)); // 1.03489
The valueOf() method
The valueOf() method returns number as string.
Example:
let x = 120;
console.log(x.valueOf()); // return 123 as string.
console.log((210).valueOf()); // return 210 as string.
console.log((120 + 80).valueOf()); // return 200 as string.
The isNaN() method
The isNaN() method returns true if the specified value is NaN.
Example:
let x = 'JavasScript';
let y = 'Script';
let result = x / y;
console.log(isNaN(result)); // output will be true
The isFinite() method
The isFinite() method determines whether passed value is a finite number or not.
Example:
let x = 1200;
console.log(isFinite(x)); // true
console.log(isFinite(x - 100)); // true
console.log(isFinite(x - 1300)); // true
console.log(isFinite("Hello")); // false
The isInteger() method
The isSafeInteger() method returns true if the passed value is safe integer, otherwise return false.
Example:
let x = 1200;
console.log(Number.isInteger(x)); // true
console.log(Number.isInteger(0.20)); // falses
console.log(Number.isInteger(x / 10)); // true
console.log(Number.isInteger("Hello")); // false
Global JavaScript Methods
JavaScript provides three global methods that can be used with any data types.
- Number()
- parseInt()
- parseFloat()
These are not the JavaScript number methods, but these are global methods.
Number()
JavaScript Number() method is used to convert various data types to the number.
Example:
console.log(Number('1')); // 1
console.log(Number('10.20')); // 10.20
console.log(Number(true)); // 1
console.log(Number(false)); // 0
console.log(Number('1000')); // 1000
console.log(Number('JavaScript')); // NaN
Note:- If the number can be converted, NaN ( Not a Number ) is returned.
Number() can also able to convert date to number.
Example:
let date = new Date();
console.log(Number(date));
parseInt()
The parseInt() method is used to convert string number to the number. parseInt() allows spaces and return only the first number.
Example:
console(parseInt('10.10')); // 10
console(parseInt('1000')); // 1000
console(parseInt('10 20 30 40')); // 10
console(parseInt('I am 22 years old')); // NaN
Note: If a number can not be converted, NaN is returned.
parseFloat()
The parseFloat() method is used to convert string number to the number. parseFloat() allows spaces and returns only the first number.
Example:
console(parseFloat('10.10')); // 10
console(parseFloat('1000')); // 1000
console(parseFloat('10 20 30 40')); // 10
console(parseFloat('I am 22 years old')); // NaN
Note: If a number can not be converted, NaN is returned.
JavaScript Number Properties
Here is the list of the JavaScript Number properties.
- MAX_SAFE_INTEGER:- Return the maximum safe integer.
- MAX_VALUE:- Return the maximum possible value.
- MIN_SAFE_INTEGER:- Return the minimum safe integer.
- MIN_VALUE:- Return the minimum possible value.
- NaN:- return the value NaN ( Not a Number ).
- POSITIVE_INFINITY:- Represent the positive infinity.
- NEGATIVE_INFINITY:- Represent the negative infinity.
JavaScript MIN_VALUE and MAX_VALUE
MAX_VALUE return the maximum possible value in JavaScript.
Example:
let value = Number.MAX_VALUE;
console.log(value); // 1.7976931348623157e+308
MAX_VALUE return the minimum possible value in JavaScript.
Example:
let value = Number.MIN_VALUE;
console.log(value); // 5e-324
JavaScript MAX_SAFE_INTEGER
MAX_SAFE_INTEGER return the maximum safe integer allow in the JavaScript.
Example:
let value = Number.MAX_SAFE_INTEGER;
console.log(value); // 9007199254740991
JavaScript MIN_SAFE_INTEGER
MIN_SAFE_INTEGER return the minimum safe integer allow in the JavaScript.
Example:
let value = Number.MIN_SAFE_INTEGER;
console.log(value); // -9007199254740991
JavaScript POSITIVE_INFINITY
POSITIVE_INFINITY represent the positive infinity.
Example:
let value = Number.MIN_SAFE_INTEGER;
console.log(value);
JavaScript POSITIVE_INFINITY
POSITIVE_INFINITY represent the positive infinity.
Example:
let value = Number.POSITIVE_INFINITY;
console.log(value);
JavaScript NEGATIVE_INFINITY
NEGATIVE_INFINITY represent the positive infinity.
Example:
let value = Number.NEGATIVE_INFINITY;
console.log(value);
Summary
In this JavaScript Number Methods tutorial, we have covered following.
- JavaScript number methods and its usages.
- JavaScript Number attributes using examples.
- Some global method that can used with any data types.
JavaScript number methods is going to be very helpful, When you want to perfomr operations on Numbers in JavaScript.
I hope this tutorial will have helped you, If you like this article, please share and keep visit or interesting JavaScript tutorials.