Here's my explaination of how to use variables.|
A variable is how you store information. So let's have this code...
var total = 0;
So that's declaring a variable called total and giving it a value of 0.
Declaring is telling the computer we are going to use a variable.
You know we're declaring something by the fact that we have the word "var".
Also sometimes we have an equals sign after the variable name and a value for the variable.
var variable_name = value;
To read this out loud you might say:
Create a variable called 'variable_name' and set it equal to 'value'.
x = 4;
Since there is no 'var' infront of this line, we presume the variable 'x' already exists
And we set x to 4.
left = right;
Whatever is on the left hand side of the equals sign is set to whatever is on the right hand side of the equals sign.
A string is something else that you can put into a variable.
var str = "Hello";
That declares a variable called str with the string 'Hello'
str = "Hi";
So say we want to store someone's name in a variable...
var name = "Jim";
var str = "Hi " + name + ", how're you?";
Weird looking expressions?
number += number
number /= number
number *= number
number -= number
++number = add 1 to number and return the new number
number++ = add 1 to number but returning the old number
--number = minus 1 to number and return the new number
number++ = minus 1 to numer but returning the old number
var c = 6;
var d = ++c; // a is 7 and b is 7
var b = 6;
var a = b++; // a is 6 and b is now 7
+ is the plus sign
/ is the divide sign
- is the minus sign
* is the times sign
a += b could also be written as a = a + b
a /= b could also be written as a = a / b
a *= b could also be written as a = a * b
a -= b could also be written as a = a - b
var c = a % b;
a mod b meaning c is now the remainder when a is divided by b.
That's it folks, enjoy.