← Return to chapter

JavaScript Basics Cheat Sheet

JavaScript, as it was modelled after other C-like languages, uses two types of comments:

// Single line comments start with double slashes
/*
Multi-line comments start with a slash asterisk and
can span
as many lines as desired.
They end with a asterisk slash.
*/

Variables and expressions

Three ways of declaring a variable legitimately (possible to just assign variable a value, but that always becomes global variable and is a bad practice)

Assignment is with the = symbol. Equality is == and will be discussed in conditionals. When you are assigning a variable, it will be internally represented as one of eight types.

Math operations follow normal algebra rules (parentheses, multiplication or division, addition or subtraction). + and - are as you expect. Division is / and multiplication is *. To get the remainder (modulus) use %. As assigning math results using a variable to that variable, such as x = x + 1, is so common, there are shortcuts:

Can also do logical operations. & is logical and, | is logical or ^ is xor >> is shift right, << is shift left. >>= and <<= are shortcuts.

Strings

Strings are immutable. Can use " or ' for strings, can have opposite symbol. For templates, ´ is used. ´used to easily show ${variable}´ length property gives number of characters in string. Common Methods:

Math functions

A holdover from Java. Math function not built in but part of library. Common variables:

methods are static so you must specify Math. Common methods:

Conditional Statements

if (condition)
    statement_to_perform_if_condition_is_true
else
    statement_to_perform_if_condition_is_false

Condition operators:

Switch Statements

Switch statements are a condensed way of having several if statements. One advantage they have over if statements is that condition code can flow through multiple cases. This is also the biggest disadvantage as it is easy to forget a break statement. The following if statement:

if (number == 1) {
    console.log("The number is one");
} else if (number == 2) {
    console.log("The number is two");
} else if (number == 3) {
    console.log("The number is three");
} else {
    console.log("The number is not one, two or three");
}

is equivalent to the following switch statement:

switch (number) {
case 1:
    console.log("The number is one");
    break;
case 2:
    console.log("The number is two");
    break;
case 3:
    console.log("The number is three");
    break;
default:
    console.log("The number is not one, two or three");
}

Functions

Functions can be defined using the function statement and return a value with return. Calling a function is done by using the name of the function

function squared(x) {
    return x * x;
}
console.log(`square of 8 is ${squared(8)}`);

They can also be assigned to a variable.

let square=function(x) {return x*x;}
console.log("square of 7 is " + square(7));

There is a shortcut (used for lambdas which will be in a future cheat-sheet)

let sqr = (x)=>x*x;
console.log("square of 6 is " + sqr(6))

Loops

Doing things multiple times, we have the for loop:

for (let i = 0; i < 10; ++i) {
    console.log(`i is ${i}.`);
}

While loops loop until a condition is met. The above for loop is essentially a macro for a while statement. here is the for loop written as a while:

let i = 0;
while (i < 10) {
    console.log(`i is ${i}.`);
    ++i;
}

Do..while loops are while loops but will always run at least one time:

i = 0;
do {
    console.log(`i is ${i}.`);
    ++i;
} while (i < 10);

Accessing Elements

We will assign elements we wish to access a unique id using the id tag as follows:

<p id="myText"></p>

Then within a script section you can access the element using something like:

let myText = document.getElementById("myText");

It lets us put something between the <p> and </p> (or whatever tag you are using). Example:

myText.innerHTML = "<b>Welcome to the <i>Universe</i>!</b>“;

Notice that the string you are using can have other tags inside of it!

Borders, and other tags that have - in them are not proper JavaScript variables so use camelCase instead

myText.style.fontSize = "24pt";
myText.style.backgroundColor = "#CAF";
myText.style.borderBottom = "5px solid red";

We will continue our coverage of JavaScript in the JavaScript Classes cheat sheet.