In the last post, we left off with the statement that JavaScript has six different basic types in it.  In this post, we will be focusing on one out of those six: Numbers. We will also go over some of the ways that we can manipulate numbers in the JavaScript console and in our programs. There will be more coding in this post so make sure to open your JavaScript console so that you can follow along. You should try entering all of the examples presented as well as anything else that strikes your curiosity and see what happens; when learning a new language it’s important to explore and use the concepts. Repetition is key to learning any new skill.

Numbers and Arithmetic

arithmeticIn every programming language, the handling of numbers is very important and this is just as true in JavaScript. Because JavaScript is untyped or dynamically typed, its interpreter considers all numbers as though they were double-precision floating point numbers. This allows us to dynamically move from integers to floats and back again. This is also why when manipulating numbers in JavaScript you will often encounter rather strange bugs as well as solutions that make little sense. Luckily, JavaScript has built-in methods that help correct some of these problems. (more on this later.)

In JavaScript, you have basic arthritic operators; addition +, subtraction -, multiplication *, division /, modulus %, increment ++, and decrement --.  You can enter an expression like 2 * 3 into the console and get 6 in return.

You may also assign numbers to variables in JavaScript. To assign a variable, you use the keyword var then the “symbol” of the variable you wish to use and you use an equal sign to assign the variable. For instance, var x = 42; will assign the number 42 to the variable x (note: in the console the semi-colon at the end of a line is not required but you should get into the habit of using one). If you now type x/3 you will get a return value of 14 because the value 42 is stored in the variable x (there will be more on variables later).

Rules and Regulations

There are some rules that are worth explaining when it comes to numbers and arithmetic in JavaScript. An integer in JavaScript is considered accurate up to 15 digits; once you go past 15 digits, some compilers will try to use scientific notation to express the number and others might try to round the number up or down.  For example, 9999999999999999 is rounded up to 10000000000000000 when entered into the console. Ultimately, you will most likely not use numbers that have this many digits because they are simply inaccurate.

console-1Decimal numbers may have up to 17 decimal places before they start to lose their accuracy.  On the same note, floating point arithmetic is pretty imprecise, to begin with in JavaScript. Try entering var x = 0.3 + 0.6 and then see what the value of x is. There are various ways around these little idiosyncrasies; for instance, var x = (0.3*10+0.6*10)/10; will give you a more accurate response. Also, division by 0 will not generate NaN, (short for Not a Number)Null, or undefined but rather infinity or -infinity depending on the values used.

You may enter a hexadecimal as a number and the compiler will automatically convert it back to decimal notation; var x = 0xEEE; will produce an integer. JavaScript also follows its own version of the order of operations. If you type 1+2*3; into the console you would think you would get 6, however, because JavaScript prioritizes multiplication over addition, you receive 7 instead.  Multiplication * and division / have higher precedence than addition + and subtraction - in JavaScript and you may change this precedence at will by using parenthesis.

Weird Basic Operators

The other three basic operators Modulus %, Increment ++ and decrement -- are very strong for certain types of programs. For those of us who did not take an advanced math class in college, a modulus of two numbers is basically the remainder of those two numbers when they are divided. Any fraction that equals a decimal number will have a non-zero modulus or remainder (this is useful for finding primes etc). For example, 30%13 will equal 4, with 30/13 equaling 2 remainder 4 or 2.30769230769…

Incrementing (and Decrementing) are used all the time in programs. When you increment (or decrement) a number you add (or subtract) a set amount to it. In the case of the operators ++ and -- you are just adding (or subtracting) 1 from each number.  If we type var z = 20; and then type ++z; into the console, it will show us 21. If we then look at the value of z, we will see that z has also been changed to 21. However, if we place the operator after the variable, z-- we will get a return value of 21 but z will have changed to 20. This happens because z returns it’s assigned value, 21 in this case before subtracting 1 from it and reassigning the new value to z.  In our original ++z, the incrementation happens before the value of z is returned, thus the new value of z is returned after z is already reassigned. This difference may seem trivial at first but as we build upon our knowledge of programming it will become rather important for sorting through more complex structures and functions.

Closing

Today’s post covered the basic operators of numbers as well as the basic properties of numbers in JavaScript.  Feel free to ask any questions if you do not fully understand any of the topics discussed here (there was a lot of content in this post to swallow at once).   Also, feel free to try anything you want to see what happens; if you come across something that wasn’t explained in the post, comment and you will receive an answer.   In our next post we will talk about three of the other types in JavaScript: Strings, Null and Undefined.