Number operations are things like addition, multiplication, division, etc. They are pretty much as you'd expect them, with a few additions. Namely things that condense common number operations.
There are a number of number operations - here are most of the relevent ones: + (addition), - (subtraction), *(multiplication), and /(division). In addition to those simple ones, there are also these: +=, -=, *=, /=, ++, and --. We can go over those after we get the simple stuff out of the way though.
Only number to number(which include variables that contain numbers and just plain numbers themselves) operations can be done using these, with the exception of additon. String to string operations can use addition, and only addition. Here are a few examples of number operations in action.
var one = 1, two = 2, three, four; var text = "Hello ", text2 = "World!", text_number = "3"; three = one + two; one = three + 1; text = text + text2; four = three + text_number; <--- illegal
Ok, lotsa stuff to explain here. Notice how I declared multiple variables on the same line. This is completely legal, and it is done just how you see it above. Just separate each delcaration with a comma. Also, you can initialize them on the same line as well, as you learned in the last lesson.
You've got the line three = one + two, which translates to three = 1 + 2 (variable one contains the number 1, and variable two contains the number 2 - don't let the badly named variables throw you off). Then, you've got one = three + 1, which works out to one = 3 + 1 (again, variable three is storing the value 3 from the last number operation). Then you have the two strings of text being added together. They would add up together to become "Hello World!". Also note that you can set a variable equal to itself plus or minus or divided by or what have you to something else like I did with text = text + text2.
Remember in the last lesson when I said stuff in quotes is treated as text even if it's just a number in the quotes? Well, I wasn't lying. The statement that is marked illegal is, in fact, illegal. It will not treat the "3" (the value of text_number) as a number, because it's in quotes. It will simply treat whatever is in variable three as a string and add them together. In this case, four would become the string "33". three equalled 3, and text_number equalled "3". So it just turned three into a string and added them up! This may play to your advantage in some cases, so it's nice to see how it's treated.
Now on to those unfamiliar number operators. Remember the text = text + text2 statement? That is where these operations come in. They condense that statement into this: text += text2. The += sign works with both text and numbers, but the others work only with numbers (or variables that contain numbers). Then there are the ++ and -- signs. They add one and subtract one, respectively. Just tack 'em on to a variable, such as one++ or ++one. These two operators are the only things that change the value of the variable without using the assignment operator (=). Position does make a difference, too. If you append it to a variable, it isn't changed until AFTER the rest of the statement has executed. If you prepend it, it will change BEFORE the statement executes. Here are some examples:
one += one; //one = one + one three /= 4; //three = three/4 two *= one; //two = two * one one += two++; // one = one + two, and then two++(two = two + 1) one += ++two; //++two(two = two + 1), and then one = one + two
The first three are what you see. They set what's on the left to whatever is done on the right. The other two are what is signifigant. Just remember that putting the ++ or -- in front of the variable makes it increment/decrement BEFORE any operations on the same line. Putting it after the variable makes it happen after any operations on the same line.
Now we have order of operations. Long operations with many different operations in them do all of the stuff in parentheses from left to right first, then all of the * and / in the statement from left to right, and finally it goes back and does all of the + and -. For example...
c = 4 + 2 - 1 / 2 * 1; //This statement observes the following order of operations //the parentheses indicate which operation is being done next c = 4 + 2 - (1 / 2) * 1; c = 4 + 2 - .5 * 1; c = 4 + 2 - (0.5 * 1); c = 4 + 2 - 0.5; c = (4 + 2) - 0.5; c = 6 - 0.5; c = (6 - 0.5); c = 5;
Unfortunately, I got a little lazy about doing the math for you and declaring variables and stuff in this lesson. Just remember to declare and define (give a value to) all variables before using them. If you feel that you need a more in-depth and specific lesson on number operations, just skip over to lesson 6 to see how to put this stuff into a web page and test it all out yourself. It's really simple stuff, and if you don't get it now it'll only take a bit of testing to see what I'm talking about.