- Want to watch a video instead?
- Your task is below the lesson
The symbols we use to do math are called operators.
The list of operators is available here.
The list of assignment operators is available here.
// Add, subtract, etc...
let x = 1;
x = x + 5;
x = 7 - 2;
x = x * 3;
x = 24 / 8;
// Exponent
let four_squared = 4**2;
let six_cubed = 6**3;
// Math Shortcuts
x = x + 3;
x += 3; // Add 3 to x's current value
x = x - 2;
x -= 2; // Reduce x's value by 2
x *= 4; // Multiply 4 into the value of x
x /= 2; // Divide x by 2 and store it back in x
// Incrementation (increase or decrease the variable by 1)
x++;
x--;
/* Anything in "quotes" is text (called a String)
* We can combine (concatenate) strings */
let first = "Mr.";
let last = "Brash";
let myName = first + " " + last; // The simple way
// It is NOT possible to subtract strings
myName = myName - "B"; // Not possible
JavaScript has a special Math object for doing complicated stuff.
Math.random()
// Get a random decimal number bettwen 0 and 1Math.round()
// Round to the nearest whole valueMath.sqrt()
// Take the square root of the number
// Calculate the area of a circle with radius of 3
let r = 3;
let area_circle = Math.PI * r**2;
// Calculate pythagorean theorem
let a = 8;
let b = 4;
let c = Math.sqrt(a**2 + b**2);
console.log("The hypotenuse of a triangle with sides", a, "and", b, "is", c)