Difference between revisions of "Variables"

From CodeStuff
Jump to: navigation, search
(Created page with "Variables are used in programming to hold values. In JavaScript you make a variable with <tt>var</tt>. <edcode> var x; print("the value of x is:"+x); </edcode> Putting a val...")
 
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
 
Variables are used in programming to hold values.  In JavaScript you make a variable with <tt>var</tt>.
 
Variables are used in programming to hold values.  In JavaScript you make a variable with <tt>var</tt>.
<edcode>
+
<edcode>var x;
var x;
+
 
print("the value of x is:"+x);
 
print("the value of x is:"+x);
 
</edcode>
 
</edcode>
 +
  
 
Putting a value into a variable is easy,  just use <tt>=</tt>.  
 
Putting a value into a variable is easy,  just use <tt>=</tt>.  
<edcode>
+
<edcode name="part2">var p;
var p;
+
 
p=153;
 
p=153;
 
print("the value of p is:"+p);
 
print("the value of p is:"+p);
 
</edcode>
 
</edcode>
 +
  
 
You can make a variable and give it a value all in one line
 
You can make a variable and give it a value all in one line
<edcode>
+
<edcode name="part3">var variableName=721;
var variableName=721;
+
 
print("the value of variableName is:"+variableName);
 
print("the value of variableName is:"+variableName);
 
</edcode>
 
</edcode>
 +
  
 
Using JavaScript, you can place any value into a variable,  
 
Using JavaScript, you can place any value into a variable,  
<edcode>
+
<edcode name="part4">var w="octopus";
var w;
+
print("the value of w is:"+w);
w="octopus";
+
print("the value of p is:"+w);
+
 
</edcode>
 
</edcode>
 +
  
 
When I said "any value" I really meant it.  Watch this one.
 
When I said "any value" I really meant it.  Watch this one.
<edcode>
+
<edcode name="part5">var z;
var z;
+
 
z=print;
 
z=print;
 
z("the value of z is:"+z);
 
z("the value of z is:"+z);
 +
</edcode>
 +
 +
 +
Changing the value of a variable is as simple as putting another variable into it.
 +
<edcode name="part6">var w=1;
 +
print("the value of w is:"+w);
 +
w=43;
 +
print("the value of w is:"+w);
 +
w=43+67;
 +
print("the value of w is:"+w);
 +
w="albatross";
 +
print("the value of w is:"+w);
 +
</edcode>
 +
 +
 +
Because variable can change value you can use them to hold information about what the program is doing right now.
 +
For example you can use them to count how many times the program has done something.
 +
<edcode name="part7">var w = 0;
 +
while (w<10) {
 +
  print("the value of w is:"+w);
 +
  w = w+1;
 +
}
 
</edcode>
 
</edcode>

Latest revision as of 08:02, 2 February 2012

Variables are used in programming to hold values. In JavaScript you make a variable with var.


Putting a value into a variable is easy, just use =.


You can make a variable and give it a value all in one line


Using JavaScript, you can place any value into a variable,


When I said "any value" I really meant it. Watch this one.


Changing the value of a variable is as simple as putting another variable into it.


Because variable can change value you can use them to hold information about what the program is doing right now. For example you can use them to count how many times the program has done something.