Search code examples
javascriptexponent

JavaScript exponent in Number literal confusion


Maybe time for another coffee, but I am seeing a strange issue that I wasn't expecting to see.

I'm reading JavaScript The Good Parts and in the grammar section I am seeing the following:

If a number literal has an exponent part, then the value of the literal is computed by multiplying the part before the e by 10 raised to the power of the part after the e. So 100 and 1e2 are the same number.

From pg. 8 of JavaScript: The Good Parts by Douglas Crockford. Copyright 2008 Yahoo! Inc., 978-0-596-51774-8.

I'm I incorrect in thinking that 2e2 should equal 400?

According to the book, shouldn't this value be (2*10)^2?

In my console it is showing me 2e2 == 200.. Is my math, reading comprehension, or anything else off? Do I need to return to basic algebra?

Thanks in advance.


Solution

  • 2e2 is interpreted as 2*(10^2) and not (2*10)^2. The former evaluates to 2 * 100 which equals 200. The latter evaluates to 20 ^ 2 which is why you are getting 400.