Search
Left Quote    If at first you don't succeed, redefine success.
- Anon.    
Right Quote
 
[login] | [Register]
 

Learning JavaScript Basics

by: bs0d
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.
  • "+" Addition
  • "-" 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 »


No Comments for this page.

You Must be logged in or a member to comment.


Tutorial Stats

Tutorial Stats

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

Options

Tutorial Options

· Login to Rate This Article
· Login to Post a Comment
· Read more by this author
Digg This Article! Del.icio.us: Bookmark This Article Reddit: Bookmark This Article BlinkList: Blink This Article! YahooMyWeb BlogMarks: Add This Mark! Furl: Save This Article Spurl: Mark This Article

Articles

Related    

· Getting Started with Ajax
Your Article Here



"AllSyntax.com" Copyright © 2002-2018; All rights lefted, all lefts righted.
Privacy Policy  |  Internet Rank