What number does 8e3 evaluate to?

前端 未结 4 1914
有刺的猬
有刺的猬 2020-12-20 11:15

I encountered this code today:

b = setTimeout(function () {
    // do some javascript stuff here
}, 8e3)

The timeout is set to 8e3

相关标签:
4条回答
  • 2020-12-20 11:47

    This is called floating point notation or scientific notation. It is 8×10³, which is 8000 miliseconds. The e stands for 10th exponent. Let me give you some more examples:

    1e3    = 1000        // 1×10³
    1e0    = 1           // 1×10⁰
    1e-1   = 0.1         // 1×10⁻¹ -> Works also for the negatives
    1.23e9 = 1230000000  // And really makes sense to shorten big numbers
    

    There is also an wikipedia article on that topic: Scientific Notation

    0 讨论(0)
  • 2020-12-20 11:55

    8e3 is eight times ten to the power of three, that is, 8000. This is called scientific (or exponential) notation. Just imagine the "e" stands for "*10^".

    In this case there isn't much point in using that notation, but once you start getting to larger numbers, exponential notation becomes more readable. To figure out how much "1000000000" is you'd have to count zeros, but 1e9 is immediately obvious.

    0 讨论(0)
  • 2020-12-20 12:02

    8e3 is exactly 8000, so the time is exactly 8 seconds.

    This could be chosen (eg. by some code minifiers) because it takes exactly 3 characters instead of 4 to represent 8000. Which means you gain 25% in terms of space in this single place :)

    And this is not "strange" notation, it is just another way of expressing numbers: Floating point notation

    0 讨论(0)
  • 2020-12-20 12:05

    8*103 = 8000 ms which is 8 seconds

    0 讨论(0)
提交回复
热议问题