You are currently viewing JavaScript Cheat Sheet 10.19 : JavaScript Number Properties

JavaScript Cheat Sheet 10.19 : JavaScript Number Properties

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.

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:

  1. Number.MAX_VALUE: Returns the largest positive finite number that can be represented in JavaScript.
  2. Number.MIN_VALUE: Returns the smallest positive finite number that can be represented in JavaScript.
  3. Number.NaN: Represents “Not-a-Number.”
  4. Number.POSITIVE_INFINITY: Represents positive infinity.
  5. 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 of maxVal.

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 of minVal.

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 of notANumber.

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 of posInfinity.

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 of negInfinity.

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!

Leave a Reply