Operators

By: aathishankaran Emailed: 1768 times Printed: 2515 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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".


Java Home | All Java Tutorials | Latest Java Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by aathishankaran
Web Security Issues
The Web User's Perspective
Server-side plug-Ins
The best way to avoid security vulnerabilities with new server
JavaScript Security
Window Object
Working with Status Bar Messages
Retrieving a Portion of a String
Referencing Windows
Math Object
Frame Object
Document Object
Closing Windows
Built-in Object in Javascript
Textarea Object

More Tutorials in Java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Java File
Java String
Count number of vowels, consonants and digits in a String in Java
Reverse a number in Java
Student marks calculation program in Java
Handling Fractions 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
Java program using Method Overriding
Java program to check if user input is an even number

More Latest News
Most Viewed Articles (in Java )
InetAddress Example program in Java
Read from a COM port using Java program
Stack example in Java - push(), pop(), empty(), search()
How to use ArrayList in Java
The Basic Structure of a Simple Java program
Using StringTokenizer in Java
FileReader and FileWriter example program in Java
Transient vs Volatile modifiers in Java
Vector example in Java
Method Overriding in Java
Method Overloading (function overloading) in Java
instanceof sample program in Java
append() in Java
Student marks calculation program in Java
Reading from a file and writing to a file using Java program
Most Emailed Articles (in Java)
Text to Speech conversion program in Java
inheritance in Java
How to initialize an Array and how to copy the array
Using Charts in JasperReports
What is Unicode?
Operator Precedence in Java
Of Object, equals (), == and hashCode ()
DateFormat sample program in Java
A Simple whois program in Java
PushbackReader sample program in Java
CharArrayReader example program in Java
ByteArrayOutputStream - sample program in Java
Vector example in Java
Arrays example in Java - asList(), binarySearch(), fill(), sort(), equals()
HashSet Sample program in Java