Important and simple JavaScript Numbers and Math functions
JavaScript has different kinds of Numbers and Math functions. Some of them are extremely important to know for every beginner JavaScript developer. We are going to learn some of them which are not only very useful but also very simple to understand. You might be using them for different mathematical solutions of code in real life. So, before we further any do. Let’s dive right in.
JavaScript Numbers Properties
JavaScript isNaN() Function
If you want to know whether a value is a number or not, you can check it so easily using JavaScript. The isNaN() function will determine whether a value is NaN (Not-a-Number) or not. You have to put some values into the opening and closing parenthesis. Let’s see an example of an isNaN() function. Open up your console and write the below code like this and press the enter button:
isNaN(123); //false
Like that, you can check more values in your console. Here are some examples of isNaN() function.
isNaN(-1.2345); //falseisNaN(123–45); //falseisNaN(0); //falseisNaN(‘12345’); //falseisNaN(‘Hello’); //trueisNaN(‘31/12/2021’); //trueisNaN(‘’); //falseisNaN(true); //falseisNaN(undefined); //trueisNaN(‘NaN’); //trueisNaN(NaN); //trueisNaN(0 / 0); //trueisNaN(null); //false
JavaScript parseFloat() Function
The parseFloat() function takes a string value and parses it into a floating-point number. That means if you put a non-decimal number, it will return an integer value. On the other hand, if you put some decimal number, it will return a floating-point number. Let’s see some examples below.
parseFloat(“10”); // 10
parseFloat("10.33"); // 10.33
parseFloat("10.00"); // 10
parseFloat("34 45 66"); // 34
parseFloat(" 60 "); // 60
parseFloat("40 years"); // 40
parseFloat("He was 40"); // NaN
JavaScript parseInt() Function
The parseInt() function can take 2 parameters, whether one parameter is a string (required) and the other parameter is radix (optional). I will cover radix in the future blog. Today we will learn just the basics of the parseInt() function. The parseInt() function takes a string value like parseFloat() function and will return only the integer value. It will never return any decimal point numbers at all. Let’s see some examples below.
parseInt(“10”); // 10
parseInt("10.33"); // 10.33
parseInt("10.00"); // 10
parseInt("34 45 66"); // 34
parseInt(" 60 "); // 60
parseInt("40 years"); // 40
parseInt("He was 40"); // NaN
JavaScript Math
JavaScript built-in Math object that allows you to perform different kinds of mathematical calculations. You can perform any mathematical tasks using the JavaScript Math object. All the functions I cover here are static, not dynamic. Let’s started.
JavaScript Math.abs() Method
The Math.abs() function takes an integer and decimal or floating-point number and returns the absolute value of that number. It just checks if the number is positive or zero. If you give a negative number, it will return the absolute positive value of it. Let’s see the example below.
The Math.round() Function
The Math.round() function only takes any positive or negative decimal point number and rounds it to the nearest integer number.
For any positive numbers, it will check any decimal point number’s last digit. If the last digit is equaled to or greater than 5, then the number will round up to the next largest integer. Otherwise, it will return downward to its nearest integer. Let’s see an example of this function below.
For any negative numbers, it will checks any decimal point number’s last digit. If the last digit is equaled to or less than than 5, then the number will round upward to its nearest integer. Otherwise, it will return downward to its nearest integer. Let’s see an example of this function below.
JavaScript Math.ceil() Function
The Math.ceil() function work as same as the Math.round() function. But it has different criteria. It will take any positive or negative numbers and will round upward to its nearest integer. Let’s see an example of this function below.
JavaScript Math.floor() Function
The Math.floor() function work as same as the Math.ceil() function. But it has different criteria. It will take any positive or negative numbers and will round downward to its nearest integer. Let’s see an example of this function below.
JavaScript Math.min() Function
The Math.min() function will take a bunch of numbers and returns the number with the lowest value. For example, you put some numbers like 23, 34, 44, 37, 11, and 12 into the function. It will return the lowest value, which is 11. Let’s see an example below.
JavaScript Math.max() Function
The Math.max() function works just like Math.min(). It will also take a bunch of numbers and returns the number with the highest value. For example, you put some numbers like 23, 34, 44, 37, 11, and 12 into the function. It will return the highest value, which is 44. Let’s see an example below.
The Math.random() Function
The Math.random() function will return a floating-point number. It will return a different number on each function call. The number range is by default between 0 (inclusive) and 1 (exclusive). This means the random number might be 0, but it will never be 1. You may change the range by multiplying some numeric value with it. Let’s see some examples below.
Example 1:
Example 2:
Example 3:
It looks like we are at the end. I hope that this article gives some beginner pieces of knowledge about JavaScript Numbers and Math. I try to explain how these functions are work. If you have any questions or suggestions, please don’t be hesitate to comment down 👇 below. Happy coding!