Numbers
Numbers are values that can be used in mathematical operations. You don’t need any special syntax for numbers — just write them straight into JavaScript.
EXAMPLE |
---|
12345; |
Decimals and fractions
JavaScript doesn’t distinguish between whole numbers and decimals, so you can use them together without having to convert from one to the other.
EXAMPLE |
---|
10 + 3.14159; |
OUTPUT |
13.14159 |
Fractions don’t exist in JavaScript, but you can rewrite them as division problems using the division operator /. Note that the resulting number is always converted to decimals — just like with a calculator.
EXAMPLE |
---|
1 / 3; |
OUTPUT |
0.3333333333333333 |
Improper fractions use the division operator in the same way.
EXAMPLE |
---|
11 / 10; |
OUTPUT |
1.1 |
To use mixed numbers, you need to add the whole number and fraction.
EXAMPLE |
---|
1 + (4 / 3); |
OUTPUT |
2.333333333333333 |
Negative numbers
You can make a number negative by placing the - operator in front.
EXAMPLE |
---|
-3; |
OUTPUT |
-3; |
You can also get a negative number by subtracting a number from a smaller number.
EXAMPLE |
---|
5 - 7; |
OUTPUT |
-2 |