Desqeh

Desqeh
Developer
Basic JavaScript code everyone should know.
The = and == and === operators
The = operator assigns a value to something, either a interger or string
Code:
function var() {
var x=0; //Assaigns x to the interger 0
var y="0"; //Assiagns y to the string "0"
//the above 2 are very different, don't be fooled by the same number in both
}
The == operator compare 2 values
Code:
function var() {
  if (x==0) { //Tests if x EQUALS 0
    if (y=="0") { //Tests if y EQUALS 0
    document.write("Both Variable Are Correct");
    }
  }
}
The === operator test if something is the same value and type
Code:
function var() {
  if (x===y) {
  document.write("The variables are wrong!");
  }
}

The other operators: !=, !==, &&, ||, ++, --
!= is used when you want to values NOT to equal each others values
Code:

function var() {
  if (x!=y) { //Tests if x DOESN'T equal y
  document.write("The 2 values don't equal each other's values");
  }
}
&& means and, it can be used to test more then one if condition
Code:

function var() {
  if (x==0 && y=="0") { //Test if x==0 AND y=="0"
  document.write("The Variables are correct");
  }
}
|| means or, it is used if 2 things can trigger an event
Code:

function var() {
  if (x==0 || y=="0") { //Tests wether x==0 OR y=="0"
  document.write("At Least one variable is correct");
  }
}
++ means increment or +1
Code:

function var() {
x++; //x+1
}
-- means decrement or -1
Code:
function var() {
x--; //x-1
}


The loops, While and For
The While loop keeps executing until a condition is false
Code:
while (x<10) {
x++;
}
The For loop repeats a certain amount of times
Code:
for (x==0;x<10;x++;) {
document.write("x equals " + x);
}
Changing a text value with something other than document.write
Code:

<html>
<body>
<script type="text/javascript">
function change() {
document.getElementById("p").innerHTML="Hello"; //This gets the elements name then innerHTML changes it
}
</script>

<p id="p" onclick="change()">Click me to change the message</p>
</body>
</html>
Changing a style sheet
Although I have never seen it used before this is a code snippet I found in a book
Code:

<html>
<head>

<style id="normal">
background-color:#B0E0E6;
</style>

<style id="green">
background-color:#008000;
</style>

</head>
<body>

<input type="button" onclick="setStyle('green')" value="Green Background">

</body>
</html>
Wow that was alot to type, feel free to ask questions and I will answer them ASAP