Number

MDN Docs for Number

Numbers

The following operations work on numbers:

operand name example =
+ addition 3 + 2 5
- subtraction 3 - 2 1
* multiplication 3 * 2 6
/ division 3 / 2 1.5
% modulus ("remainder") 3 % 2 1
** exponentiation 3 ** 2 9

LAB: Playing With Numbers

Answer the following questions using node:

Order of operations

Q: What is 1 plus 2 times 3?

Order of operations

Q: What is 1 plus 2 times 3?

A: It depends!

(1 + 2) * 3 == 9
1 + (2 * 3) == 7

Parentheses Are Free

When in doubt, use parentheses!

LAB: one nation, divisible

One number is divisible by another if you divide them and the remainder is 0.

Write code in node to figure out if...

Strings vs. Numbers

Hmmm....

1 + 2
"1" + "2"
"1 + 2"

Strings plus Numbers

Hmmm again...

"1" + 2

Even though Strings and Numbers are different TYPES, JavaScript converts one to the other.

But when two types meet, which one wins?

Type Conversion

The clearest answer is that you, the programmer, explicitly declare which type you want to win.

Converting a Number to a String

(12).toString() // "12"

Converting a String to a Number

There are many ways to convert a string to a number in JavaScript.

The easiest and cleanest is unary +:

expression value
+"12" 12
+"012" 12
+"0.2" 0.2
+"cheese" NaN
+"0" 0
+"" 0
+" " 0

(Other ways can give bizarre results if the string contains letters or begins with a 0.)

Type Coercion

If you don't explicitly do type conversion then JavaScript will "helpfully" convert the types for you.

This is called type coercion and just like with people, coercion is stressful and often backfires.

For instance, if you ask the user their age, and read it from a web form or keyboard input, it will be in a string. If you forget to explicitly convert that to a number before using it in a calculation, the results can be unexpected...

  > "30" - 1              // "30" is coerced into a number
  29            
  > "30" + 1              // 1 is coerced into a string
  '301'

(You Don't Know JS has way more information than you wanted to know on this subject.)

Math is hard

There are many types of numbers!

Each is useful in different situations.

In elementary school, we learned

In high school, we learned irrational, complex, and imaginary numbers.

JavaScript numbers are different from all of those!

Floating-Point Numbers

JavaScript numbers...

Some rational numbers cannot be represented in floating-point, which means that simple arithmetic may give unexpected results.

For instance, you can't go higher than about 9 quadrillion without glitching...

> 2**53 == 2**53+1
true

(This problem is not unique to JavaScript, but the fact that all numbers in JS are floating-point means that beginners can't avoid it.)

see Wikipedia on IEEE 754 double aka Double-precision floating-point format or binary64

Arithmetic

Try this in node:

1 + 2
3 - 4
5 * 6
7 / 8
2 / 3
7 / 9

Wait a second...

> 2/3
0.6666666666666666
> 7/9
0.7777777777777778

Why did it round up the last digit for 7/9 but not for 2/3?

Floating Point Arithmetic is Bizarre

> 7/9 
0.7777777777777778

> 0.5 - 0.4 - 0.1
-2.7755575615628914e-17

> (0.8 - 0.7 - 0.1)/(0.5 - 0.4 - 0.1)
-3

> 2**53 == 2**53+1
true

> 2**10000
Infinity

Okay, that's enough math for now!

>> Variables >> << Strings <<

Outline

[contents]

Numbers Slides - Code Like This

/

#