1

If you want to return the base to the exponent's power, you could either use Math.pow(base, exponent) or base ** exponent.

This works for small numbers like 20 ** 5, but if you want slightly bigger numbers, 20 ** 200 for example, you only get the first few numbers. Thus, in this case 1.6069380442589902e+260. And if you want even bigger numbers, such as 20 ** 20000, JavaScript returns Infinity.

I want to return the exact number instead of only the first 17s. I know this is possible in Python; but how can I do it in JavaScript?

  • 1
    JavaScript does not have intrinsic support for large numbers. Depending on what you are trying to do, there may be better technologies to use. – James McLeod Oct 09 '17 at 14:40
  • 1
    This might help: https://stackoverflow.com/questions/307179/what-is-javascripts-highest-integer-value-that-a-number-can-go-to-without-losin – Rajesh Oct 09 '17 at 14:40
  • `20 ** 200` isn't a safe number in JavaScript. The question @Rajesh linked above goes into a bit more detail about what that means. – James Donnelly Oct 09 '17 at 14:41

1 Answers1

0

For such large numbers, JS native numbers won't be enough; you could use some "Big Number" library, like, for example:

Haroldo_OK
  • 6,612
  • 3
  • 43
  • 80