Guide to numbers and math functions in JavaScript
1. Converting a String to a number
JavaScript provides the following methods to convert strings to numbers
Number()
: This method converts the given argument to a number.parseInt()
: Converts the given string to an integer.parseFloat()
: Returns the floating-point number after parsing the given string.
Example:
var num = '10.25';
console.log(Number(num));
console.log(parseInt(num));
console.log(parseFloat(num));
Output:
10.25
10
10.25
2. Creating a number object
You can also create an object whose data type is a number using the Number()
costructor.
Example:
var num = new Number(45);
console.log(num);
console.log(typeof num.valueOf());
Output:
Number { 45 }
number
3. Checking if a value is a number or not
isNaN()
can be used to check if a value is a number or not. "NaN stands for Not a Number". isNaN()
returns false if a value is a number and true if a value is not a number.
Example:
var num1 = new Number(45);
var num2 = new Number('abc');
console.log(isNaN(num1));
console.log(isNaN(num2));
Output:
false
true
4. Fixing the number of digits
toPrecision()
and toFixed()
methods are used to fix the number of digits in a number.
toPrecision()
: will limit the total number of digits in a number.
toFixed()
: will limit the total number of digits that appear after the decimal point in a floating-point number.
Example:
var num1 = 23.347389
console.log(num1.toPrecision());
console.log(num1.toPrecision(5));
console.log(num1.toFixed(5))
Output:
23.347389
23.347
23.34739
Note: toFixed()
performs the rounding off operation to shorten the number as you can see in the example above.
5. Absolute value of a number
Math.abs()
is used to find the absolute value of a number in javascript.
Example:
console.log(Math.abs());
console.log(Math.abs(-5));
Output:
NaN
5
6. Finding the maximum number
Math.max()
returns the maximum number of the provided numbers.
Example:
console.log(Math.max(1, -20));
console.log(Math.max(40, 50, 20, -8, 109));
Output:
1
109
7. Square root of a number
Math.sqrt()
returns the square root of the given argument.
Example:
console.log(Math.sqrt(9));
console.log(Math.sqrt('abc'));
Output:
3
NaN
8. Floor and Ceil
Math.floor()
rounds off the number to the closest lowest integer.
Math.ceil()
rounds off the number to the closest highest integer.
Example:
console.log(Math.floor(9.4));
console.log(Math.ceil(9.4));
Output:
9
10
9. Length of a number
Unlike strings, there is no method to find the length of a number in javascript. However, you can write your own code to find the length of a number.
Example:
var num = 8389457834;
var len = Math.log(num) * Math.LOG10E + 1 | 0;
console.log(len);
Output:
10
10. Swapping two numbers
This is one of the common questions which are repeatedly asked in an interview.
10.1. Using a temporary variable
You can swap the value of two variables using a third temporary variable by exchanging their values one by one.
Example:
var num1 = 45;
var num2 = 90;
console.log("Before Swapping num1 : " +num1+" num2 : "+num2);
var temp;
temp = num1;
num1 = num2;
num2 = temp;
console.log("After swapping num1 : " +num1+" num2 : "+num2);
Output:
Before Swapping num1 : 45 num2 : 90
After swapping num1 : 90 num2 : 45
10.2. Without temporary variable
You can also swap values of two variables without a temporary variable.
Example:
var num1 = 45;
var num2 = 90;
console.log("Before Swapping num1 : " +num1+" num2 : "+num2);
num2 = num2 - num1;
num1 = num1 + num2;
num2 = num1 - num2;
console.log("After swapping num1 : " +num1+" num2 : "+num2);
Output:
Before Swapping num1 : 45 num2 : 90
After swapping num1 : 90 num2 : 45
11. Conclusion
There are a lot of methods you can use in javascript while working with numbers in javascript which makes using numbers in javascript quite easy.