BucksWeb.org JS CheatSheet

Variables & Strings

Make a String (text) variable

let name = "olivia";

Make an Integer (whole number) variable

let age = 20;

Update a variable

age = 30;

Concatenate Two Strings

let first_name = "ash";
let last_name = "ketchum";
let full_name = firstname + " " + last_name;

//This should output "ash ketchum"
console.log(full_name);

Input / Output

Output to the Console

console.log("hello world!");

Create a Pop-up Box (Alert) These can get spammy, use sparingly.

alert("hello world!");

Loops

Basic While Loop

while (age > 40) {
    //If age doesn't change, you're in an infinite loop!
    age = age + 1;
}

Basic For Loop

for (age = 0; age < 40; age = age + 1) {
    //Age will start at 0 and "increment" by 1 each loop!
    console.log(age);
}

Basic Conditionals (IF Statements)

Basic If Statement

if (age > 40) {
    console.log("You're old!");
}

Basic If ... Else Statement

if (age < 40) {
    console.log("You're young!");
} else {
    console.log("You're old!");
}

Else If Statement

if (age < 5) {
    console.log("You're a baby!");
} else if (age < 18 ) {
    console.log("You're a minor!");
} else {
    console.log("You're an Adult!");
}

Equal

if (age == 20) { ... }

Less Than

if (age < 20) { ... }

Greater Than

if (age > 20) { ... }

NOT Equal

if (age != 20) { ... }

Greater than OR Equal To

if (age >= 20) { ... }

OR

if (age == 20 || age == 30) { ... }

AND

if (age == 20 && name == 'olivia') { ... }