Number
.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 ("power of") | 3 ** 2 | 9 |
Answer the following questions using node
:
Q: What is 1 plus 2 times 3?
Q: What is 1 plus 2 times 3?
A: It depends!
(1 + 2) * 3 == 9
1 + (2 * 3) == 7
When in doubt, use parentheses!
(or, when there's any doubt, make your assumptions explicit)
One number is divisible by another if you divide them and the remainder is 0.
Write code in node
to figure out if...
Hmmm....
1 + 2
"1" + "2"
"1 + 2"
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?
The clearest answer is that you, the programmer, explicitly declare which type you want to win.
(12).toString() // "12"
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.)
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.random is your way to make things unpredictable!
Calling Math.random()
returns a random decimal number between 0 and 1.
If you want a random integer you can multiply that decimal by a range, like this:
function randomInteger(min, max) {
let range = max - min + 1;
return min + Math.floor(Math.random() * range);
}
console.log(randomInteger(1, 3));
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!
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
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
?
> 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
/