Numbers

Our first data type!

Three ways of looking at numbers

(And every other data type.)

  1. What values can be represented.

  2. What the syntax is for writing them in our langauge.

  3. What we can do with them.

What values can be represented?

Numbers are infinite.

Computers are finite.

What to do?

Like many things in computers, we compromise

Javascript uses a format that is well supported by modern computers, called binary floating point.

Basically scientific notation for computers.

Good general purpose format

It can represent integer values from -253 to 253 (-9,007,199,254,740,992 to 9,007,199,254,740,992)

It can represent decimal values and very large numbers with approximately 15 decimal digits of precision.

It can also represent very small numbers near zero with greater precision.

What does this mean for us?

We don’t really have to worry about it. 🎉

(Most of the time.) 🤨

How do we write numbers?

0

1

2

3

1000000000

1_000_000_000

9_007_199_254_740_992

Nine quadrillion seven trillion one hundred ninety-nine billion two hundred fifty-four million seven hundred forty thousand nine hundred ninety-two

-1

-2

0.0

1.2

-0.5

1.4142135623730951

(Best approximation of the square root of 2)

2.718281828459045

(Best approximation of e)

3.141592653589793

(Best approximation of π)

1.7976931348623157e+308

(Biggest number we can represent.)

1.7976931348623155e+308

(The second biggest number we can represent.)

The gap between it and the biggest is 1.99584030953472e+292

2.220446049250313e-16

5e-324

Infinity

NaN

What can we do with numbers?

Math, mostly.

What does it even mean to do things with numbers?

A lot of programming is about combining smaller pieces into bigger pieces.

Expressions

Expressions are a way of combining values (like numbers) with operators to produce new values.

The exact operators depend on the types of the values.

Some operators on numbers

+

-

*

/

%

**

» 1 + 2
3

» 10 - 6
4

» 5 * 10
50

» 50 / 10
5

» 1 / 3
0.3333333333333333

Note, this is an approximation.

» 0.1 + 0.5
0.6

» 0.1 + 0.2
0.30000000000000004

wtf?!

» 23 % 12
11

» 52 % 7
3

» 2 ** 8
256

» 2 ** 55
36028797018963970

Uh, oh. That’s not right. 💩

Should be 36028797018963968

Would be better as 3.602879701896397e16

» 2**70
1.1805916207174113e+21

» 1 + 2 * 3
7

» (1 + 2) * 3
9

Wrapping up

Two kinds of values

Individual numbers are values.

Expressions combine numbers to produce new values.

The same value can be written different ways

12345

12_345

12345.00

1.2345e4

Different expressions can have the same value

1

43 / 43

(108 - (2 ** 3)) * 0.01

We’ll talk more later about what you can do with values.

Up next

Booleans