Number
.Number
represents decimal numbers, such as 0
, 1
, 42
, 3.14159
, or -2.71828
.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 interactive node
, or a JavaScript file.
Question: What is 1
plus 2
times 3
?
Answer: It depends on if there are parenthesis or not.
(1 + 2) * 3 == 9
1 + (2 * 3) == 7
4 + 5 * 6 ** 2 / 4
(((4 + 5) * 6) ** 2) / 4
When in doubt, use parentheses to make operation order explicit. Alternatively, break the expression up, and use variables.
One number is divisible by another if you divide them and the remainder is 0.
Write some code to check the following
100
is divisible by 5
100
is divisible by 8
330
is divisible by 11
337
is divisible by 7
Hint: remember the
modulus
operator%
Sometimes Strings and Numbers combine in ways that are unexpected.
1 + 2
"1" + "2"
"1 + 2"
In the above examples, Strings are coerced into Numbers. Why?
Coercion of Strings to Numbers, or Numbers to Strings can be confusing.
Even though Strings and Numbers are different TYPES, JavaScript will convert one to the other.
"1" + 2
// 12
Why do you think the Number is converted to a String this time?
Instead of depending on implicit coercion, you can explicitly convert a Number to a String, or vice-versa.
(12).toString()
// "12"
Can you think of other ways to convert a Number to a String?
There are also many ways to convert a String to a Number
One of the easiest and cleanest is the unary +
operator
expression | value |
---|---|
+"12" | 12 |
+"012" | 12 |
+"0.2" | 0.2 |
+"cheese" | NaN |
+"0" | 0 |
+"" | 0 |
+" " | 0 |
See also: MDN: String to Number Conversion
Some large numbers cannot be represented accurately in floating-point.
For instance, look closely at the results below.
2 ** 53
// 9007199254740992
2 ** 53 + 1
// 9007199254740992
This problem is not unique to JavaScript, but is more obvious because all numbers are floating-point decimals. More information at IEEE 754 Double-Precision
Try the following in node
7 / 8
// 0.875
2 / 3
// 0.6666666666666666
7 / 9
// 0.7777777777777778
Why would
7 / 9
round up, but2 / 3
not?
Try the following in node
0.1 + 0.2
// 0.30000000000000004
0.3 - 0.2 - 0.1
// -2.7755575615628914e-17
Why are these calculations inaccurate?
/