JavaScript Tutorial
6. Operators
JavaScript has many different operators, which come in several flavors, including binary. This tutorial will cover some of the most essential assignment, comparison, arithmetic and logical operators.
6.1. Selected assignment operators
An assignment operator assigns a value to its left operand based on the value of its right operand. The basic assignment operator is equal (=), which assigns the value of its right operand to its left operand. The other operators are shorthand for standard operations. Find an abridged list of shorthand operators below:
Shorthand operator | Meaning |
---|---|
x += y | x = x + y |
x -= y | x = x - y |
x *= y | x = x * y |
x /= y | x = x / y |
Note that the = sign here refers to assignment, not "equals" in the mathematical sense. So if x is 5 and y is 7, x = x + y is not a valid mathematical expression, but it works in JavaScript. It makes x the value of x + y (12 in this case).
EXAMPLES: If x = 10 and y = 2, x += y would mean x = x + y, hence x's new value would be the sum of x's previous value plus y's previous value. Upon executing x = x + y = 10 + 2, x's new value becomes 12, while y's new value remains equal to its old value. Similarly, x -= y would change x's value to 8. Calculate x *= y and x /= y to get a better idea of how these operators work.
6.2. Comparison operators
A comparison operator compares its operands and returns a logical value based on whether the comparison is true or not. The operands can be numerical or string values. When used on string values, the comparisons are based on the standard lexicographical ordering. They are described in the following table.
Operator | Description | Example |
---|---|---|
Equal (= =) | Evaluates to true if the operands are equal. | x == y evaluates to true if x equals y. |
Not equal (!=) | Evaluates to true if the operands are not equal. | x != y evaluates to true if x is not equal to y. |
Greater than (>) | Evaluates to true if left operand is greater than right operand. | x > y evaluates to true if x is greater than y. |
Greater than or equal (>=) | Evaluates to true if left operand is greater than or equal to right operand. | x >= y evaluates to true if x is greater than or equal to y. |
Less than (<) | Evaluates to true if left operand is less than right operand. | x < y evaluates to true if x is less than y. |
Less than or equal (<) | Evaluates to true if left operand is less than or equal to right operand. | x <= y evaluates to true if x is less than or equal to y. |
EXAMPLES: 5 == 5 would return TRUE. 5 != 5 would return FALSE. (The statement 'Five is not equal to five.' is patently false.) 5 <= 5 would return TRUE. (Five is less than or equal to five. More precisely, it's exactly equal to five, but JavaScript could care less about boring details like that.)
6.3. Selected Arithmetic Operators
Arithmetic operators take numerical values (either literals or variables) as their operands and return a single numerical value. The standard arithmetic operators are addition (+), subtraction (-), multiplication (*), division (/) and remainder (%). These operators work as they do in other programming languages, as well as in standard algebra.
Since programmers frequently need to add or subtract 1 from a variable, JavaScript has shortcuts for doing this. myVar++ adds one to the value of myVar, while myVar-- subtracts one from myVar.
EXAMPLES: Let x = 3. x++ bumps x up to 4, while x-- makes x equal to 2.
6.4. Logical Operators
Logical operators take Boolean (logical) values as operands and return a Boolean value. That is, they evaluate whether each subexpression within a Boolean expression is true or false, and then execute the operation on the respective truth values. Consider the following table:
Operator | Usage | Description |
---|---|---|
and (&&) | expr1 && expr2 | True if both logical expressions expr1 and expr2 are true. False otherwise. |
or (||) | expr1 || expr2 | True if either logical expression expr1 or expr2 is true. False if both expr1 and expr2 are false. |
not (!) | !!expr>/td> | False if expr is true; true if expr is false. |
EXAMPLES: Since we have now learned to use the essential operators, we can use them in conjunction with one another. See if you can work out why the following examples resolve the way they do: