DEV Community

Cover image for How to make Infinity!
MAFEE7
MAFEE7

Posted on • Updated on

How to make Infinity!

New Method to make Infinity!

So... I found a new way to make Infinity in Javascript!

This is how:

Using parseInt() with .repeat()! 🤣

Meaning of parseInt()

parseInt() basically converts a string into Integer. If some invalid string is passed (like: "hi"), it returns NaN

Meaning of .repeat()

string.repeat() returns the same string but repeated the number of times given!

Using it to make Infinity ∞!

Code:

   parseInt(`${"9".repeat(999)}`)
Enter fullscreen mode Exit fullscreen mode

So if you just use

   "9".repeat(999)
Enter fullscreen mode Exit fullscreen mode

It will return something like this:
image
More the 9's more the number's digits! (But I used 999)

Now, parseInt converts the string with the 999 9's into an integer. The integer is so big that javascript treats it like Infinity!

Yay we got Infinity in javascript!

😳 I found this when trying to make a string with millions of Hi!s

[New] Easier way

Math.pow(9999, 9999)
Enter fullscreen mode Exit fullscreen mode

Top comments (5)

Collapse
 
_bkeren profile image
''

parseInt(${"9".repeat(304)}) returns a number , parseInt(${"9".repeat(305)}) returns Infinity

Collapse
 
mafee6 profile image
MAFEE7

Cuz that's where the number exits the limit
Hence infinity

Collapse
 
_bkeren profile image
''

but that's interesting, because 304 is not power of 2.

Collapse
 
mafee6 profile image
MAFEE7

btw there is a hard limit on the number you can use in repeat also!

Collapse
 
mafee6 profile image
MAFEE7

😳