It's not accurate! It's very close to, but not exactly, the correct result of 0.3
This is not a mistake. It's actually how all fractional calculations happen in computers. The reason is relatively complex, but in brief, computers do not have unlimited memory, and so only calculate fractions to a limited degree of precision. Consider calculating (2 / 3) * 3
to only 6 decimal places. 2 / 3
would calculate to 0.666667
. Multiplying that by 3 would give 2.000001
- close to, but not exactly, the intended answer of 2. In computers this type of rounding happens in binary (base-2, or all 0s and 1s), which means it happens in different situations, such as with 0.1 + 0.2
.
As programmers, we just have to live with this. Often fractional calculations will be slightly wrong, but sometimes it doesn't matter, or the programmer makes sure that values within a small range are deemed acceptable so rounding errors don't affect the result.
Special numbers
Numbers in JavaScript can hold three possible special values: positive infinity (Infinity
), negative infinity (-Infinity
), and "Not A Number" (NaN
).
Positive and negative infinity work as you may expect:
// Try entering in to the console:
1 / 0 // Infinity
-1 / 0 // -Infinity
Infinity + 1 // Infinity
"Not A Number", or NaN
, is less obvious. It is a result used when the result cannot be mathematically represented, such as the square root of a negative number, or an invalid calculation.
// Try entering in to the console:
0 / 0 // NaN
In general if you see NaN
it means you made a mistake in a calculation. You might need to check your maths is correct, or add extra checks to make sure you aren't attempting to calculate something like 0 / 0
. This is not always as obvious as it seems: you might use variables such as numberOfSweets / numberOfPeople
, and if both variables are 0, the result will be NaN
.
Strings
We've already used strings, but let's cover a few more details.
Why do we refer to text in code as a string? The name is short for "string of characters", which is what this data type really is - a sequence of individual characters, one after another. The name string is usually used to refer to the data type for text in computer programming.
String can use either '
, "
or `
characters to mark the string, so long as the start and end characters are the same. All three examples below are equivalent.
"Hello world!"
'Hello world!'
`Hello world!`
It doesn't matter which you use, but it's a good idea to be consistent. This guide generally uses double-quotes for strings.
If you want to include a double-quote inside a double-quoted string, you can use a backslash character \ to escape it. That means the double-quote is part of the text, rather than marking the end of the string.
// Try entering in to the console:
"Hello \"world\"!" // Hello "world"!
This also means backslashes themselves must be escaped as \\. There are various other escape sequences you can use, like \t for a tab, and \n for a new line.
An alternative is to use a different quote for the string, such as single quotes, e.g. 'Hello "world"!'
.
We'll cover more about strings in the next part of this guide.
Booleans
A boolean value can only be either true
or false
. This is useful for on/off values, such as settings, as well as making comparisons.
let b = true;
console.log(b);
We'll come back to booleans later in the guide as they have some key uses in JavaScript programming, particularly with comparisons and if
statements. However for now, it's just good to be aware that booleans are another data type in JavaScript.