If at first you don't succeed, redefine success.- Anon.
 News Tutorials Code Samples Blogs Forums

Page: 5 of 10
(View All)

### OPERATORS, IF AND IF/ELSE

Now is a good time to discuss operators in JavaScript. I am also doing to discuss If and If/Else statements because this is when you are going to use many of these operators. There are several types of operators, and they are listed and described below:

Arithmetic Operators: These are they symbols used to calculate values.
• "-" Subtraction
• "*" Multiplication
• "/" Division
• "%" Modulus (Takes one number and divides it by another number and places the remainder in the variable.)
If you are unfamiliar with Modulus, here is an example of how it works:

In arithmetic conditions, JavaScript follows the order of operations. I think in grade school they taught it as something like, "Please Excuse My Dear Aunt Sally."
• Parenthesis ()
• Unary Operations -
• Mult. and Division (L to R) * and /
• Add and subtraction (L to R) + and -

It is useful to be aware of the order of operations when working with more than 2 numbers. Here is an example of what I mean:

Relational Operators: These are used when comparing one or more items and will return a Boolean expression.
• "==" Equals
• "<" Less Than
• ">" Greater than
• ">=" Greater than or Equal
• "<=" Less Than or equal
• "!=" Not equal

Logical Operators: used to compare two or more simple conditions at the same time.
• "&&" = and
• "||" = or

Something to go along with the logical operators are what are called, "Truth Tables." Truth tables show all possibilities for logical operators when looking at conditions.

 &&: ||: X Y X&&Y X Y X||Y F F F F F F F T F F T T T F F T F T T T T T T T

Compound Conditions: This is just where you combine two or more simple conditions with a logical operator.

Example:

#### INCREMENT OPERATOR:

To say, x = x + 1:
++x;
x++;

Say you took: y = x++; What's going to happen?
y will = x
and then x will = x + 1.

Say you took y = ++x; What's going to happen?
x will = x + 1
then, y = x.

#### DECREMENT OPERATOR:

To say, x = x - 1:
--x;
x--;

If you took y = x--; What will happen?
y will = x
and then x will = x - 1.

If you had y = --x; What happens?
x will = x - 1;
then y will = x.
Not as confusing as you might think. This will help you when using increment and decrement operators to know the value of your variable through the process of the program.

 1  |  2  |  3  |  4  |  5  |  6  |  7  |  8  |  9  |  10  | Next »

You Must be logged in or a member to comment.

#### Tutorial Stats

391,518 Views
4.66 Rating of 5 (3 Votes)