### JavaScript Operators, Expressions and Evaluation

An operator is a symbol used for evaluation of an expression. That definition would make more sense if you knew the meaning of expression and evaluation, right? Keep reading...

An expression is any combination of values, variables, operators or other expressions. An operand is a variable or literal on either side of an operator. An operator is a symbol (or sometimes word) used to manipulate expressions.

Evaluation is the process of determining the outcome, or result. When an expression is said to have evaluated to a value, the process has been completed. In the simple mathematical statement, "2 + 2", the entire statement is an expression, the plus sign is the operator, and the expression evaluates to 4. If I used a different operator as in, "2 - 2", the expression would evaluate to something else. So in another sense, operators are used to manipulate operands.

Operators are classified into several categories: arithmetic, assignment, comparison, logical, string and 3 special operators. For now, focus only on familiarizing yourself with them. We'll study how they are used more in depth in a later lesson.

#### Arithmetic Operators

used to perform mathematical calculations
+ plus
- minus
* multiplication
/ division
% modulus or remainder

#### Assignment Operators

used to assign a value to a variable
= left operand equals right operand
a = b a equals b
+= left operand equals itself plus right operand
a += b a is equal to itself plus b, OR a = a + b
-= left operand equals itself minus right operand
a -= b a is equal to itself minus b, OR a = a - b

#### Comparison Operators

compare two operands for equality
== is equal to
!= is not equal to
>  greater than
<  less than
>= greater than or equal to
<= less than or equal to

#### Logical Operators

compare two Boolean operands for equality (Boolean value is either true or false):
&& and
a == 2 && b == 2 both a and b are equal to 2
|| or
a == 2 || b == 2 a is equal to 2 or b is equal to 2
! not
!aa is not true, or a is false

#### String Operators

combines strings
+ concatenation operator, combines one string with another
+= concatenation operator, combines additional string to itself
Yes, those operators are the same as the 2 in other categories listed above. What makes them string operators is that they are used with strings. Lessons in the following String Tutorial go into great detail about string operators.

#### Operator Precedence

When in doubt, use parenthesis. That's as complex as I'm going get because that's all you need to remember. Rather than trying to remember operator precedence hierarchy (which operands matched with which operators to be evaluated first), using parenthesis insures that the correct result will be returned, especially if you get into a complex operation.

In the following example, I'm going to force precedence (meaning I will force one expression to evaluate before the others) just by placing parenthesis around it.
a + b - (x / y) != g - l
In the above example, the expression to the left of not-equal-to (!=) operator contains a sub expression which must be evaluated first in order to carry out an accurate evaluation. Notice the extra set of parens around x / y. They are forcing x to be divided by y before being subtracted from a + b. Go to Netscape's DevEdge Online Reference for a complete listing of JavaScript operators and for more information on expressions and operators.

>>Functions
>>JavaScript Variables
>>JavaScript Arguments
>>JavaScript Operators, Expressions and Evaluation
>>Special JavaScript Operators
>>Decision Making: If Statement, Else Clause

View Source | Home | Contact