Close
Close full mode
logoMakeCode AP CSP

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; //variable
b = "init"; //string
varc 8; //int
var d false; //Boolean
var e[1,2,3,4,5]; //array
const 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, subtract
A = b * (c / d); //multiply, divide
X = 100 % 48; // modulo, 100 / 48 remainder = 4
a++; b--; //postfix increment/decrement

Other

a = b //assignment
a == b //equals
a != b //not-equal
a === b //strict equals
a < b, a > b, a =< b, a => b //less/greater
a && b //logical and
a || b //logical or

References

JavaScript Language:

Tutorials at W3schools.com
Reference at W3schools.com

πŸ“˜ Unit 3 - AAP Part 1 β€” Previous
Levels of Programming Languages
Next β€” πŸ“˜ Unit 3 - AAP Part 1
Code Tracing and Rewrite in JavaScript