Coding for Kids

Variable Type: Number

You may have heard of these before. Yep, they're just like regular numbers. 317 is a number. 42.19 is a number. 0.00034 is a number, and so is 2.33×10-7. (That last one is in scientific notation and the computer would show it as 2.33e-7.)

A number without a decimal is called an integer, or int for short.

A number with a decimal is called a floating-point number, or float for short.

Other computing languages have additional number types, like double precision, signed sbyte and unsigned byte, signed short and unsigned ushort integers, and so on.

Declaring a Number

To make a number variable, you simply create a variable using let and assign it a numerical value.

Syntax:

Examples:

As Booleans

In JavaScript, you can use a number in a Boolean check. All numbers are considered to be true, except for zero (0), which is evaluated as false.

Exponents

If you have really large or really small numbers, you may need to make use of an exponent as part of scientific notation. The e that you see in the molecules example above is one such case. This is Avogadro's number (the number of parts in one mole of a substance) and it gets interpreted as 6.02214×1023. In decimal form, it's: 602,214,000,000,000,000,000,000. That's a lot of zeros!

Can you see what the 1023 is doing? It's acting as a placeholder, so we don't have to write all those zeros. If you move that decimal all the way to the left so it's immediately to the right of the 6, you'd have to move it 23 times. The computer shows this as 6.02214e23. You may have seen this notation on your calculator too.

If you had two million dollars, it would be $2,000,000. How many digits are after the 2? There are 6 of them, so this would become $2×106, which is 2e6 in your code.

If the number is really small, we use a negative exponent. The relative mass of a proton is 1 atomic mass unit, but its mass in kilograms is about 1.67×10-27. So take that decimal there and move it to the left 27 times to get: 0.000,000,000,000,000,000,000,000,001,67 kg. For the computer, we would use 1.67e-27 to represent the same thing.

Incidentally, if you do ever have to type in a long number, never use commas. They wouldn't work. Those numbers above should be: 602214000000000000000000 and 0.00000000000000000000000000167 if you were writing them into your code. (But just use the exponent versions instead.) Good luck not missing a zero.

Parsing

Sometimes you have a variable that's a string variable and you need to convert it in the computer to a number, so it can be used for calculations. This commonly happens when we pull information from an input box on a website or a prompt popup. The user might type in a number, but the computer pulls it in as if it's a word (made of digits). For these, we need to parse the string. JavaScript has two ways to do this, depending on whether you want an integer or a float (decimal).

The parse commands in JavaScript will take a string and read it from left to write. It will take any number it sees until it sees a letter or non-number character. Then it stops reading the string. If the first character it sees is a letter, then it can't parse the string and you get NaN (Not a Number). If a number is split with a space, it stops parsing there.

Notice that the parseInt() function does not round the number. It truncates—or removes, like Math.floor()—any decimal it sees. If you run parseInt(99.99999); you get 99. (Look at the Math object page for rounding.)

Not a Number: NaN

This is what you get when you try to parse a string of letters or if you try to do a math operation (aside from +) on a non-numbered string, like "hi" * 3. This does not give you "hihihi". The NaN value is an odd one and it's not simple to use. You can't test a variable as NaN by checking like this if (someNumber === NaN) It doesn't work. There is a special function whose sole purpose is to test for a value being NaN. It's easy to get the capitalization wrong on it.

Getting Rid of e

Sometimes you want to simplify the appearance of a number. I made use of this on my website a lot. If you have 9e4, for example, it's kind of silly. Why not just put 90000? JavaScript lets you do this with the Number() constructor. You can use it like function. Note: the N is capital. If the exponent is rather large or small, it will still keep the e notation.

Putting e in there

You can use the .toExponential() method to convert a value into scientific notation. This can only be used on a number variable. You can also send a parameter in to control the number of decimal places to show, properly rounded.

Fixing your Decimal Places

Use .toFixed(number) to control the number of decimal places.

Significant Figures

A significant figure can be thought of as a directly-measured value. There are rules for whether or not a digit is considered significant.

Use .toPrecision(number) to write a number to a specified length. If you want an answer to three significant figures, use someNumber.toPrecision(3).

Use of significant figures

Coding for Kids Sidenote: "Sig figs" are often used in chemistry and physics, but they're not the only places. Generally, you may use them to shorten a long number, like 87234.76536876 or the value of π which goes on forever. Often, when using .toPrecision(), you will get a result with an exponent (e) in it. Just wrap it in the Number() function to convert it a standard number.
  • (87234.76536876).toPrecision(3);           //8.72e4
  • Number(87234.76536876).toPrecision(3);      //87200

Significant figures are used to maintain a certain level of precision in measurements and calculations. For example, if you measure your weight, then you put a sprinkle of salt on your hand, you would say your weight doesn't really change. This is because the scale isn't precise enough to measure grains of salt.

Rules for significant figures

Computation rules for significant figures

When adding or subtracting values, add or subtract as normal, then keep the same number of decimal places as the least precise number, rounding properly.

When multiplying or dividing values, multiply or divide as normal, then keep the total number of significant figures as the least precise number, rounding properly.

Numbers are infinitely interesting!

—Dr. Wolf