What is Number.MIN_VALUE in JavaScript?
JavaScript has only one type to store numerical values. In this shot, we discuss the MIN_VALUE property of JavaScript Number, which returns the smallest positive numerical value possible.
Syntax
var num = Number.MIN_VALUE;
Number.MIN_VALUE returns 5e-324, which is the smallest positive numerical value in JavaScript closest to zero. You can create a variable and store it for use.
Code
The following is the code to print the value returned by Number.MIN_VALUE.
var num = Number.MIN_VALUE;console.log(num);
The value returned by Number.MIN_VALUE is greater than zero. Let’s check if this is true or not.
var num = Number.MIN_VALUEconsole.log(1> num > 0)
- The first line gives the numerical value closest to zero, which we get from
Number.MIN_VALUE. - In the second line, we have a condition to check if
numis greater than zero or not.
Finding the max element in an array
The example below demonstrates how to use Number.MIN_value to find the maximum value in an array.
var num = Number.MIN_VALUE;let array = [23,52,12,31,4322,121,442]for(var i of array){if(i > num){num = i;}}console.log(num)
In the example above, we are looking for the maximum element of the array. We use Number.MIN_VALUE to initialize a variable that will be compared to the elements of the array using for loop.
-
In line 1, we store the value returned by Number.MIN_VALUE in a variable named
num. -
In line 3, we declare an array of size 7 with random values.
-
In line 5, we iterate through the array using for loop.
-
In lines 6 and 7, we have an if condition to check if the value is greater than
numor not. If it is greater than num, we replace the value with the current value in the num variable. -
In line 11, after the completion of the
forloop we get the greatest number from the array in thenumvariable.