Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

javascript
communitycreator

What is Number.MIN_VALUE in JavaScript?

Ayush Trivedi

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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_VALUE
console.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 num is 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 num or 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 for loop we get the greatest number from the array in the num variable.

RELATED TAGS

javascript
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring