You will want to bookmark this page for future reference.
|+ ||Addition ||2 + 4|
|- ||Subtraction ||6 - 2|
|* ||Multiplication ||5 * 3|
|/ ||Division ||15 / 3|
|% ||Modulus ||43 % 10|
Modulus % may be a new operation to you, but it's just a special way of saying "finding the remainder". When you perform a division like 15/3 you get 5, exactly. However, if you do 43/10 you get an answer with a decimal, 4.3. 10 goes into 40 four times and then there is a leftover. This leftover is what is returned by the modulus operator. 43 % 10 would equal 3.
Performing operations on variables that contain values is very common and easy to do. Below is a simple script that performs all the basic arithmetic operations.
var two = 2
var ten = 10
var linebreak = "<br />"
document.write("two plus ten = ")
var result = two + ten
document.write("ten * ten = ")
result = ten * ten
document.write("ten / two = ")
result = ten / two
Comparisons are used to check the relationship between variables and/or values. A single equal sign sets a value while a double equal sign (==) compares two values. Comparison operators are used inside conditional statements and evaluate to either true or false. We will talk more about conditional statements in the upcoming lessons.
|Operator||English ||Example ||Result|
| == ||Equal To ||x == y ||false|
| != ||Not Equal To ||x != y ||true|
| < ||Less Than ||x < y ||true|
| > ||Greater Than ||x > y ||false|
| <= ||Less Than or Equal To ||x <= y ||true|
| >= ||Greater Than or Equal To ||x >= y ||false|
Found Something Wrong in this Lesson?
Report a Bug or Comment on This Lesson - Your input is what keeps Tizag improving with time!