Mastering JavaScript Number Properties
Introduction
Welcome to the world of programming with JavaScript! In this tutorial, we will delve into the fundamentals of JavaScript Number Properties, a crucial aspect of JavaScript programming. Understanding these properties is essential for performing various operations on numbers in your programs.
![](https://dumudigitikakenya.com/wp-content/uploads/2023/11/JavaScript-Number-Properties-1024x576.jpg)
Table of Contents
JavaScript Number Properties
JavaScript provides several properties for working with numbers. These properties offer valuable information about numeric values and can be utilized in various scenarios. In this tutorial, we will focus on key JavaScript Number Properties, namely:
- Number.MAX_VALUE: Returns the largest positive finite number that can be represented in JavaScript.
- Number.MIN_VALUE: Returns the smallest positive finite number that can be represented in JavaScript.
- Number.NaN: Represents “Not-a-Number.”
- Number.POSITIVE_INFINITY: Represents positive infinity.
- Number.NEGATIVE_INFINITY: Represents negative infinity.
Sample Codes and Explanations
1. Number.MAX_VALUE
let maxVal = Number.MAX_VALUE;
console.log(maxVal);
Explanation:
Number.MAX_VALUE
returns the largest positive finite number in JavaScript.- The variable
maxVal
stores this maximum value. - The
console.log()
statement displays the value ofmaxVal
.
Output:
1.7976931348623157e+308
2. Number.MIN_VALUE
let minVal = Number.MIN_VALUE;
console.log(minVal);
Explanation:
Number.MIN_VALUE
returns the smallest positive finite number in JavaScript.- The variable
minVal
stores this minimum value. - The
console.log()
statement displays the value ofminVal
.
Output:
5e-324
3. Number.NaN
let notANumber = Number.NaN;
console.log(notANumber);
Explanation:
Number.NaN
represents “Not-a-Number” in JavaScript.- The variable
notANumber
stores this special value. - The
console.log()
statement displays the value ofnotANumber
.
Output:
NaN
4. Number.POSITIVE_INFINITY
let posInfinity = Number.POSITIVE_INFINITY;
console.log(posInfinity);
Explanation:
Number.POSITIVE_INFINITY
represents positive infinity in JavaScript.- The variable
posInfinity
stores this infinite value. - The
console.log()
statement displays the value ofposInfinity
.
Output:
Infinity
5. Number.NEGATIVE_INFINITY
let negInfinity = Number.NEGATIVE_INFINITY;
console.log(negInfinity);
Explanation:
Number.NEGATIVE_INFINITY
represents negative infinity in JavaScript.- The variable
negInfinity
stores this infinite negative value. - The
console.log()
statement displays the value ofnegInfinity
.
Output:
-Infinity
Conclusion
Congratulations! You’ve successfully explored the basics of JavaScript Number Properties. These properties play a crucial role in numeric operations and handling special values in your JavaScript programs. Keep practicing and applying these concepts in your projects to solidify your understanding. Stay tuned for more advanced JavaScript tutorials!