The most common JavaScript Number Methods
Primitive values can not have properties and methods because they are not objects. However, with JavaScript, methods, and properties are also available to primitives because JavaScript treats primitive values as objects when executing methods and properties. These methods make manipulating and modifying numeric values much easier.
1. toString() Method
The toString() method converts a number into a string.
let year = 2021;
year.toString(); // "2021"
2. toExponential() Method
This method returns a string with a number rounded and written using exponential notation.
let num = 9.656;
num.toExponential(2); // returns 9.66e+0
The parameter defines the number of characters behind the decimal point. The parameter is optional, if it has not been specified, JavaScript will not round the number.
3. toFixed() Method
The toFixed() method rounds up a number to the nearest highest or lowest fixed-point notation and returns a string with a number written with a specified number of decimals. It takes in a parameter that signifies the number of digits that should be displayed after the decimal point.
let num = 9.656;
num.toFixed(0); // returns 10
num.toFixed(2); // returns 9.66
4. toPrecision() method
The toPrecision() method returns a string with a number written with a specified length. It takes an argument that signifies the length. If a specific length is not given, the method returns the number as it is.
let num = 9.656;
num.toPrecision(); // returns 9.656
num.toPrecision(2); // returns 9.7
num.toPrecision(3); // returns 9.66
num.toPrecision(6); // returns 9.65600
5. valueOf() method
The valueOf() method simply returns a number as a number.
let x = 24;
let num = x.valueOf();
console.log(num); // 24
console.log(typeof num); // Number
6. Number() method
The Number() method is used to convert JavaScript variables into numbers. If the number cannot be converted, NaN (Not a Number) is returned.
Number(true); // returns 1
Number(false); // returns 0
Number("10"); // returns 10
Number("10.33"); // returns 10.33
Number("10,33"); // returns NaN
Number("10 33"); // returns NaN
Number("Treasure"); // returns NaN
7. parseInt() method
The parseInt() method passes a string and formats it into an integer. Spaces are allowed and only the first number is returned. If the number cannot be converted, NaN (Not a Number) is returned.
parseInt("20"); // returns 20
parseInt("20.66"); // returns 20
parseInt("20 40 60"); // returns 20
parseInt("20 years"); // returns 20
parseInt("years 20"); // returns NaN
8. parseFloat() method
The parseFloat() method passes a string value and returns a number with its decimal value. Spaces are allowed and only the first number is returned. If the number cannot be converted, NaN (Not a Number) is returned.
parseFloat("20"); // returns 20
parseFloat("20.66"); // returns 20.66
parseFloat("20 40 60"); // returns 20
parseFloat("20 years"); // returns 20
parseFloat("years 20"); // returns NaN
The main difference between parseInt() and parseFloat() methods, is that when a string is passed, parseFloat() returns the number with its decimal values where as parseInt() only returns the integer.
9. isInteger() method
It determines whether a given value is an integer and returns a boolean value. It returns true if the value is an integer, otherwise, it returns false.
let num = 10;
let result = Number.isInteger(num);
console.log(result); // returns true
let num = 10.77
let result = Number.isInteger(num);
console.log(result); // returns false
let num = "10"
let result = Number.isInteger(num);
console.log(result); // returns false
10. isFinite() method
It determines whether a given value is a finite number and returns a boolean value. It returns true if the value is a finite number, otherwise, it returns false.
let num = 10;
let result = Number.isFinite(num);
console.log(result); // returns true
let num = - 10.22;
let result = Number.isFinite(num);
console.log(result); // returns true
let num = "10";
let result = Number.isFinite(num);
console.log(result); // returns false
11. toLocaleString() method
The method returns a number as a string value according to a browser's locale settings.
let num = 100;
console.log(num.toLocaleString()); // returns "100"
Conclusion
Number methods are used to perform different tasks on numbers like manipulating numerical values.