In this comprehensive chapter, we will explore the various properties associated with numbers in JavaScript. From basic properties like NaN and Infinity to more advanced concepts like Number.MAX_VALUE, we'll cover everything you need to know about properties of numbers in JavaScript.
JavaScript provides several basic properties that are associated with numbers.
NaN
(Not a Number)NaN
represents a value that is not a valid number.
const result = 10 / 'hello';
console.log(result); // Output: NaN
console.log(typeof result); // Output: "number"
Infinity
and -Infinity
Infinity
represents a value that is greater than any other number, while -Infinity
represents a value that is smaller than any other number.
console.log(1 / 0); // Output: Infinity
console.log(-1 / 0); // Output: -Infinity
JavaScript also provides advanced properties related to numbers.
Number.MAX_VALUE
and Number.MIN_VALUE
Number.MAX_VALUE
represents the maximum numeric value representable in JavaScript, while Number.MIN_VALUE
represents the smallest positive numeric value greater than zero.
console.log(Number.MAX_VALUE); // Output: 1.7976931348623157e+308
console.log(Number.MIN_VALUE); // Output: 5e-324
Number.POSITIVE_INFINITY
and Number.NEGATIVE_INFINITY
These properties represent positive and negative infinity, respectively.
console.log(Number.POSITIVE_INFINITY); // Output: Infinity
console.log(Number.NEGATIVE_INFINITY); // Output: -Infinity
JavaScript distinguishes between 0
and -0
, although they both represent zero.
console.log(1 / -0); // Output: -Infinity
console.log(1 / 0); // Output: Infinity
console.log(-0 === 0); // Output: true
console.log(Object.is(-0, 0)); // Output: false
Number.EPSILON
Number.EPSILON
represents the difference between 1 and the smallest floating-point number greater than 1.
console.log(Number.EPSILON); // Output: 2.220446049250313e-16
Understanding the properties associated with numbers in JavaScript is crucial for writing robust and reliable code. From basic properties like NaN and Infinity to advanced properties like Number.MAX_VALUE and Number.EPSILON, mastering these concepts will make you a more proficient JavaScript developer.Experiment with these properties in your code, and remember to handle special cases and edge cases appropriately. By mastering the properties of numbers, you'll be better equipped to tackle numerical challenges in your JavaScript projects. Happy coding !❤️