Numbers and Arithmetic
++, and decrement
--. You can enter an expression like
2 * 3 into the console and get 6 in return.
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
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.
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)
undefined but rather
-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;
* and division
/ have higher precedence than addition
+ and subtraction
Weird Basic Operators
The other three basic operators Modulus
++ 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
-- 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.