JavaScript Infinity PROPERTYIn JavaScript, Infinity is a special number with an intriguing property: greater than any finite number. We might be amazed at how infinite numbers operate in conditional statements and arithmetic operations. This operation works if we don't know the properties of Infinity beforehand. Let's look at the Infinity number's properties in JavaScript, acknowledge the use cases, and avoid common pitfalls. Positive Infinity and negative Infinity are two other special values. Note the program expressions "+Infinity" (Infinity) and "Infinity" produce these two infinite Number values. Positive InfinityThe number is the initial value of the Infinity. POSITIVE INFINITY is greater than any other finite number. It differs from mathematical Infinity in the following ways.
Syntax The following example shows the POSITIVE INFINITY method. Example The following example shows the POSITIVE INFINITY method. We can use positive numbers and return values. Output The image shows simple infinity information as an output. Negative InfinityIn JavaScript, there is also a negative Infinity (Infinity) with the value Number. NEGATIVE INFINITY. Infinity is less than any finite number. In the following ways, it differs from mathematical Infinity:
Syntax The following example shows the NEGATIVE INFINITY method. Example The following example shows the NEGATIVE INFINITY method. Output The image shows negative infinity information as an output. Checking for InfinityThe Number.isFinite() method checks whether the passed number is a finite number. It is a number that is neither positive Infinity, negative Infinity, or NaN. Syntax The following syntax shows an infinite value. Parameters
Return Value
Example The following example shows the INFINITY method. Output The image shows negative and positive information as an output. When Should We Use Infinity?The infinity value is useful for starting computations that involve number comparisons. For example, when looking for the smallest value in an array, we could initialize the minimum variable with Inifinity. Because every finite value is less than Infinity, the minimum value will become the first item on the first for() iteration. Pitfall of InfinityWe won't be dealing with Infinity values very often. However, it is important to understand when infinite attributes may appear. There are multiple ways to get the infinity values.
Parsing NumberAssume we have a form that collects information from users. We have a field on the form where users can enter their input_val.
Syntax The following syntax applies the parseFloat() function. The parseFloat() function recognizes the Infinity, which returns an infinite number. When an input field contains the string ' Infinity,' it is best to practice generating a validation error. Example The following example shows the parseFloat() function. We can use the positive and negative infinity data. The function returns a finite and infinite value using the parseFloat() Function. Output The image shows all types of infinity information as an output. JSON SerializationJSON.stringify() converts an Infinity number to a null value. Syntax The following syntax shows the JSON Serializationmethod.
Example The following example shows the JSON Serializationmethod. Output The image shows the infinity value as an output. Maximum Number OverflowsMAX VALUE is JavaScript's largest float number. When attempting to use a number greater than a number MAX_VALUE, JavaScript converts it to Infinity. We can operate mathematical operations like exponential, multiplication, and so on. Syntax The following syntax shows the maximum number overflows method. Example The following example shows the maximum number overflows method. Output The image shows all types of infinity information as an output. Mathematical operationsIn JavaScript, some functions in the Math namespace can return infinite numbers. When a function works without any arguments, then Math.max() returns the "Infinity", and Math.min() returns the "Infinity". We may be surprised if JavaScript tries to find the maximum or minimum of an empty array. Syntax The following syntax shows the mathematical operation for the infinity property. Example The following example gives the infinity property using the mathematical operation. Output The image shows all types of infinity information as an output. Most Important Takeaway
ConclusionThe infinity property or data handles in JavaScript for mathematical operations and largesize data. It is an easy and useful method for users and developers to handle infinite properties or data.
Next TopicJavaScript insertAdjacentHTML() method
