JavaScript Commands
Commands
If - Else
If (true) {Status = true;} else [Status = false;
Switch statement:
var num = <input>Switch (num) {case O:document.write("zero");case 1:document.write("one");default:document.write("welp");
Variables
var a; //variableb = "init"; //stringvarc 8; //intvar d false; //Booleanvar e[1,2,3,4,5]; //arrayconst PI = 3.14; //constant
Functions
Function addNumbers(a,b) {return a + b;}X = addNumbers(1,2);
Loops
For loop:
for (var i βO; i < 10; i++) {document.write("this will happen 10 times");}
While loop:
var i = 1;while(i < 100) {i*=2;document.write(i);}
Do while loop:
var i = 1;do {i*=1;document.write(i);} While (i < 100);
Operators
A= b + c - d; //Add, subtractA = b * (c / d); //multiply, divideX = 100 % 48; // modulo, 100 / 48 remainder = 4a++; b--; //postfix increment/decrement
Other
a = b //assignmenta == b //equalsa != b //not-equala === b //strict equalsa < b, a > b, a =< b, a => b //less/greatera && b //logical anda || b //logical or
References
JavaScript Language: