## Operators

**By:** aathishankaran **Viewed:** 153199 times Printer Friendly Format

**Assignment Operators**

An operator you already know is the assignment operator.
Its most basic function is assigning a value to a variable, thereby placing the
value in memory.

For
example, the expression x = 20 assigns the value 20 to the variable x. When
JavaScript encounters the assignment operator (=), it first looks to the right
for a value. It then looks to the left and ensures that there is a place to
store the number. If it finds a variable, it assigns the value to it. In this
case, x holds the value of 20. It always works from right to left, so the
expression 20 = x causes an error in JavaScript by trying to assign a new value
to 20. This is not allowed, given the fact that 20 is not a variable, but an
integer whose value cannot be changed.

JavaScript supports 11 other assignment operators that are
really a combination of the assignment operator and either an arithmetic or
bitwise operator. These shorthand versions follow:

**Combination of Assignment and Arithmetic Operators**

x += y is short for x = x + y

x -= y is short for x = x - y

x *= y is short for x = x * y

x /= y is short for x = x / y

x %= y is short for x = x % y

**Combination
of Assignment and Bitwise Operators**

x <<=y is short for x = x << y

x >>= y is short for x = x>>y

x > > > = y is short for x = x > > > y

x &= y is short for x = x & Y

x ^ = y is short for x = x ^ Y

x |=
Y is short for x = x | y

**Arithmetic Operators**

When working with numbers, you use arithmetic operators.

Operators |
Usage |

+ |
Addition |

- |
Subtraction |

* |
Multiplication |

/ |
Division |

% |
Modulas |

++i is the same as using i = i + 1

--i is the same as using i = i - 1

**Comparison Operators**

Comparison operators are used for just that, comparing.
Expressions that use comparison operators are essentially asking a question about
two values. The answer can either be true or false.

Operators |
Usage |

== |
Equals |

!= |
Not Equal |

> |
Greater than |

>= |
Greater than or equal |

< |
Less than |

<= |
Less than or equal |

**Conditional Operators**

JavaScript uses the set of two operators, ? and :, to form conditional expressions.

Conditional
expressions return one of two values based on the logical value of another
expression. For example, you can use the following conditional expression to
alert the user if he is the millionth person to view the page

resultMsg == (numHits=1000000) ?"You have won!" : "You lost. Try again!"

alert
(resultMsg)

This expression returns the string "You have won!" if num Hits is equal to 1000000; otherwise, it returns, "You lost. Try again!" To put this idea to work, the second line of the previous example displays the" result to the user using the built-in alert () function. If numHits is equal to one million, an alert dialog box pops up to let the visitor know.

**String Operators**

The set of string operators includes the concatenate
operator (+), which is also used as the arithmetic operator for addition, and
all comparison operators. Using the concatenate operator, you can easily attach
strings together to make a larger one.

**Boolean
Operators **

Boolean operators (also called logical operators) are used in conjunction with expressions that return logical values. The following is a list of the three Boolean Operators :

&&The logical and operator returns true if both Expression! and Expression2 are

true.
Otherwise, it returns false. Note the following examples:

(1>0) && (2>1) returns true.

&& (2<1) returns false.

|| The logical or operator returns true if either
Expression! or Expression2 is true. If neither Expression! nor Expression2 is
true, then it returns false. Note the following examples:

(1>0) || (2<1) returns true.

(1<0) || (2<1) returns false.

! The logical not operator is an unary operator that returns the opposite value of

Expression.
If Expression is true, it returns false; and if Expression is false, it returns
true. This works in the same way as the arithmetic negation operator and will
not permanently change the value of Expression. Note the following examples:

!(1>0) returns false.

!(1<0) returns true.

**The typeof Operator**

The typeof operator returns the type of data that its operand currently holds.

This
is especially useful for determining if a variable has been defined. Note the
Following examples:

typeof unescape returns the string "function".

typeof undefinedVariable returns the string "undefined".

typeof 33 returns the string "number".

typeof "A String" returns the string "string".

typeof true returns the string "boolean".

typeof null returns the string "object".

Comment on this tutorial

- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview

#### categories

#### Subscribe to Tutorials

#### Related Tutorials

Update contents of a file within a jar file

Tomcat and httpd configured in port 8080 and 80

Count number of vowels, consonants and digits in a String in Java

Student marks calculation program in Java

Calculate gross salary in Java

Calculate average sale of the week in Java

Vector in Java - Sample Program

MultiLevel Inheritance sample in Java

Multiple Inheritance sample in Java