Lesson 2 of 199:22 video

JavaScript data types and operators

How a row of zeros and ones becomes a value with a type, what operators do with those values, and why JavaScript says 0.1 + 0.2 isn't 0.3.

In this lesson

  1. 0:00 Intro
  2. 0:39 One pattern, two meanings
  3. 1:37 Values and types
  4. 2:24 Text
  5. 3:27 Nothing at all
  6. 3:43 True or false
  7. 5:25 Arithmetic
  8. 6:15 Strange numbers
  9. 6:56 Why 0.1 + 0.2 isn't 0.3
  10. 8:53 Next time

Transcript

Intro

Type zero point one plus zero point two into JavaScript, and the answer isn't zero point three. It's zero point three, then fifteen zeros, then a four. How does a language running on almost every website get that sum wrong? Today, we find out.

By the end of this video, you'll understand three things. One: how a row of zeros and ones becomes a value with a type. Two: what operators do with those values. Three: why that sum came out wrong.

One pattern, two meanings

Look at this row of zeros and ones. Each digit is a bit, short for binary digit, and physically it can be as simple as a tiny switch that's either off or on. Photos, music and messages all end up stored this way. On its own, this row doesn't mean anything yet. To get something out of it, you need to know how to read it.

One way is to read it as a number. We count in base ten, where each column is worth ten times the column to its right: ones, tens, hundreds. Binary works the same way, with twos instead of tens. Starting from the right, the columns are worth 1, 2, 4, 8, and so on, doubling each time up to 128. A 1 means count this column, and a 0 means skip it. This row has a 1 in the 64 column and a 1 in the 1 column, so it adds up to 65.

Values and types

Computers also need to store text, and text gets stored as numbers too. A long list gives every character a number, and number 65 is a capital A. So the same row can mean 65 or capital A, depending on how you read it. The bits can't tell you which. So when you write JavaScript, you rarely touch bits directly. You work with values, and each one carries a type, which says what kind of thing it is.

You can ask JavaScript for a value's type with an operator called type of. An operator takes one or more values and hands you back a new one. Type of 65 gives you number. Put quotes around a capital A, and type of gives you string, which is what JavaScript calls text.

Text

Quotes are what make a string. Put them around 65, and you no longer have a number, just the characters 6 and 5. JavaScript accepts three kinds of quote mark: double, single and the backtick. Pick whichever you like, but close the string with the same mark you opened it with.

That list of characters is called Unicode, and it goes well beyond the alphabet. It covers writing systems from around the world, along with symbols and emoji, and each character's number is called a code point. This emoji is number 128,512. JavaScript keeps those numbers in an encoding called UTF-16, so underneath, a string is a row of numbers, and those numbers are bits.

Operators work on strings too. Put a plus sign between two strings and nothing gets added up. They get joined, and the formal word for that is concatenation. The Way, plus Tech, plus Works gives you a single string.

Nothing at all

JavaScript even has values for nothing. Think of a blank on a form. If nobody has filled it in yet, that's undefined. If someone deliberately wrote none, that's null. The two mean different things, but plenty of code treats them alike.

True or false

The next type has only two possible values, true and false, and it's called Boolean. Comparing two numbers gives you one: 10 greater than 2 is true, and 3 greater than 5 is false. Three equals signs in a row ask whether two values are the same. 65 against 65 is true, but 65 against the string 65 is false, because their types differ. These operators take a value on each side, so they're called binary operators. Binary just means made of two: a binary digit has two possible values, and a binary operator takes two values. Type of takes only one, which makes it a unary operator.

Three more operators combine Booleans, and a door makes them easy to picture. AND is a door with two locks, which opens only when both keys turn. OR is a door with a key and a keypad, where either one will do. NOT is the sign on the door that flips from open to closed: it turns true into false, and false into true. AND and OR take two values, so they're binary. NOT takes one, so it's unary.

JavaScript has one operator that takes three values, so it's called the ternary operator. It works like a fork in the road. You start with a true or false value, then a question mark, then two options with a colon between them. True takes you down the first road, and false takes you down the second. So 10 greater than 2, question mark, yes, colon, no, gives you yes. Change it to 3 greater than 5, and you get no.

Arithmetic

That leaves numbers, and the operators here are the ones you'd expect: plus, minus, an asterisk for times and a slash for divide. They follow the order you learned at school too. One sandwich at 5 and two coffees at 3 come to 5 plus 2 times 3, which is 11, because the multiplication happens first. Wrap the addition in parentheses to make it go first, and the result becomes 21.

The percent sign is the one you might not know. It gives you the remainder of a division, so it's called the remainder operator, or sometimes modulo. Share 17 slices of pizza between 5 people, and everyone gets 3, with 2 left over. That 2 is what the operator gives you. Share 20 slices between 5, and nothing's left, so you get 0.

Strange numbers

Some sums have no ordinary answer, and JavaScript gives them their own values. Divide 1 by 0 and you get Infinity, or negative Infinity if you divide minus 1 by 0. A result too big to store becomes Infinity as well. And Infinity stays Infinity, so adding 25 to it changes nothing.

Divide 0 by 0, or take Infinity away from Infinity, and there's no sensible answer at all. JavaScript returns NaN, short for Not a Number. Ask type of about NaN, and it says number. It's the number type's way of saying a calculation failed.

Why 0.1 + 0.2 isn't 0.3

Now for that wrong answer from the start. JavaScript gives every regular number exactly 64 bits, in a format called floating point. Scientific notation writes a huge number as a few digits and a power of ten, and floating point does the same thing in binary. So the 64 bits are divided up: one for the sign, eleven for the power, which is called the exponent, and fifty-two for the digits.

That layout can reach enormous sizes, but the digits only get a fixed amount of room. The largest whole number JavaScript can store reliably is about 9 quadrillion, which is this 16-digit number. Go past it and numbers start getting rounded, so adding 1 can leave the answer exactly where it was.

Fractions hit the same wall much sooner. Those binary columns from earlier keep going to the right of the point, halving each time: a half, a quarter, an eighth, a sixteenth. A binary fraction has to be built from those pieces, and no combination of them adds up to exactly one tenth. It's the same problem one third has in decimal, where the threes never end and you have to stop somewhere. With fifty-two bits of digits, JavaScript stops and rounds.

So zero point one gets stored as a number very slightly above zero point one, and zero point two has a similar tiny error. Add them, and the errors are just big enough to show: zero point three, fifteen zeros and a four. Nothing is broken. Other languages using the same format give the same answer. Usually the difference is too small to matter. When it does matter, as with money, programs often count in whole cents, because whole numbers stay exact.

Next time

That's values, types and operators in one line: bits mean nothing until a type says how to read them, and operators turn those values into new ones. Next time, we put them to work in real programs, with variables, decisions, loops and functions.

Learn more